使用 PowerShell 列出所有 Windows 服务器的所有服务器角色
您准备记录您的网络和操作系统环境吗?您不想购买第三方软件吗?您想要列出网络中所有服务器的所有服务器角色吗?开始吧……使用 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 列出所有已安装的角色和功能
享受记录的乐趣!

