แสดงรายการบทบาทเซิร์ฟเวอร์ทั้งหมดจากเซิร์ฟเวอร์ Windows ทั้งหมดด้วย PowerShell
คุณกำลังจะจัดทำเอกสารเครือข่ายและสภาพแวดล้อมระบบปฏิบัติการของคุณหรือไม่? คุณไม่ต้องการซื้อซอฟต์แวร์บุคคลที่สามใช่ไหม คุณต้องการแสดงบทบาทเซิร์ฟเวอร์ทั้งหมดจากเซิร์ฟเวอร์ทั้งหมดในเครือข่ายของคุณหรือไม่? เอาล่ะ … ด้วย PowerShell มีอะไรอีกบ้าง? ?
ฉันคาดหวังอะไรจากโพสต์บล็อกนี้ ลองดูภาพด้านล่าง หากนี่คือสิ่งที่คุณกำลังมองหา อ่านต่อ...
Windows Server รองรับการติดตั้งคุณสมบัติและบทบาทที่เรียกว่า คุณลักษณะมีความเกี่ยวข้องเฉพาะในพื้นที่เท่านั้น บทบาทคือบทบาทของเครือข่าย เช่น DHCP, DNS และอื่นๆ
คุณสามารถคัดลอกและวางโค้ดได้ที่นี่ หรือคุณสามารถดาวน์โหลดสคริปต์ทั้งหมดได้ในตอนท้ายของโพสต์นี้และในส่วนการดาวน์โหลดของฉัน
รหัส: ดึงข้อมูลบทบาทเซิร์ฟเวอร์ทั้งหมด
นี่มันคือ. คัดลอกและวางบรรทัดลงใน PowerShell ISE หรือ Visual Studio Code แล้วดึงทริกเกอร์ ซึ่งโดยปกติจะกด F5
โดยวิธีการ: สคริปต์นี้ได้รับการทดสอบใน PowerShell 5.1 และ PowerShell 7
# .SYNOPSIS
# This script collects all installed Server Roles and Features
# from all domain-joined Windows Servers.
# .DESCRIPTION
# The result is displayed in the console and a report will be
# saved in $HOME\ServerRoles"Date".txt.
# The file will open automatically.
# .NOTES
# Run this script with administrative privileges
# on a domain-controller of your domain.
# Supported PowerShell versions: PowerShell 5.1, PowerShell 7
# .AUTHOR
# Patrick Gruenauer | Microsoft MVP PowerShell [2018-2020]
# .WEB
# https://sid-500.com
############################ CODE ###############################
### Retrieving all Servers by Name
$ErrorActionPreference = "SilentlyContinue"
$servers = (Get-ADComputer -Properties operatingsystem `
-Filter 'operatingsystem -like "*server*" -and enabled -eq "true"').Name
### Collection Point
$result = @()
### Ping all Member-Server and if ping is successful run
# Get-WindowsFeature
foreach ($item in $servers) {
$test = Test-Connection $item -Count 1
### Providing PowerShell 7 and 5.1 compatibility in terms of return code
If ($test.Status -eq 'Success' -or $test.StatusCode -eq '0')
{
$roles = Get-WindowsFeature -ComputerName $item |
Where-Object InstallState -EQ 'Installed'
### Write into the collection point
$result += New-Object -TypeName PSObject -Property ([ordered]@{
'Server' = $item
'Roles' = $roles.Name -join "`r`n"
### Escape Character CarriageReturn (`r) + NewLine (`n)
}
)
}
}
### Generating Outputs
# Collecting Point: Console Output
Write-Output $result | Format-Table -Wrap
# Text File Output
$date = Get-Date -Format MM.dd.yyyy
$result | Format-Table -Wrap |
Out-File "$HOME\ServerRoles$date.txt"
Write-Warning "Output file ServerRoles$date.txt generated in $HOME ... Opening file ..."
# Opening File
Start-Process $HOME\"ServerRoles$date.txt"
ตามที่กล่าวไว้ ให้คัดลอกโค้ดด้านบนลงในโปรแกรมแก้ไข PowerShell ที่คุณเลือก หรือดาวน์โหลดสคริปต์ในส่วนดาวน์โหลดของเว็บไซต์ของฉัน
สำหรับการเรียกข้อมูลบทบาทของเซิร์ฟเวอร์ในแต่ละเซิร์ฟเวอร์ โปรดดู Windows Server: แสดงรายการบทบาทและคุณสมบัติที่ติดตั้งทั้งหมดโดยใช้ PowerShell
ขอให้สนุกในการจัดทำเอกสาร!

