如何通过PowerShell查询WSUS中指定单台服务器的Windows更新状态
问题原因
你写的单行命令无法运行是因为Get-WsusUpdate不支持直接接收Get-WsusComputer的管道输入,需要通过WSUS计算机对象自带的方法查询单台设备的更新状态。
修改后的单台服务器查询脚本
# 定义要查询的目标服务器名称和报告输出路径 $targetComputerName = "替换成你要查询的服务器全称" $reportPath = "C:\script\$targetComputerName`_更新报告.html" $date = Get-Date # 连接WSUS服务器,远程查询的话把localhost改成WSUS服务器地址,端口对应修改 $server = Get-WsusServer -Name "localhost" -PortNumber 8530 # 获取目标服务器的WSUS对象,NameIncludes是模糊匹配,要精确匹配可以换成NameEquals $targetComputer = Get-WsusComputer -UpdateServer $server -NameIncludes $targetComputerName if (-not $targetComputer) { Write-Error "未在WSUS中找到名为$targetComputerName的计算机,请检查名称是否正确" exit } # 筛选需要的更新:分类为严重/安全、状态为失败/待安装、未被拒绝 $updateList = $targetComputer.GetUpdateInstallationInfoPerUpdate() | Where-Object { $_.Update.Classifications | Where-Object {$_.Title -in "Critical Updates", "Security Updates"} -and $_.UpdateApprovalAction -ne "Declined" -and $_.InstallationState -in "Failed", "NotInstalled" } | Sort-Object -Property @{e={$_.Update.Title}} # 组装报告数据 $updates = @() foreach ($updateInstall in $updateList) { $updateInfo = $updateInstall.Update $props = @{ '更新名称' = $updateInfo.Title '更新ID' = $updateInfo.Id.UpdateId '安装状态' = $updateInstall.InstallationState } $updobj = New-Object -TypeName PSObject -Property $props $updates += $updobj } # 生成HTML内容 $updatesHtml = $updates | ConvertTo-Html -Fragment -PreContent "<h2>$targetComputerName 待安装严重/安全更新</h2><p>生成时间:$date</p>" $head = @" <title>$targetComputerName WSUS更新状态报告</title> <style> body { background-color: #FFFFFF; font-family: sans-serif; } h1, h2 { color: #1E87F0; } table { border-collapse: collapse; width: 100%; } td { background-color: #FFFFFF; color: #666666; padding: 3px; border: 1px solid #1E87F0; } th { background-color: #1E87F0; color: #FFFFFF; text-align: left; padding: 3px; border: 1px solid #1E87F0; } </style> "@ # 输出报告文件 ConvertTo-Html -Head $head -Body $updatesHtml | Out-File $reportPath -Encoding UTF8
可用的单行查询命令
$server = Get-WsusServer localhost 8530; $c = Get-WsusComputer -UpdateServer $server -NameIncludes "你的服务器名"; $c.GetUpdateInstallationInfoPerUpdate() | Where-Object {$_.InstallationState -in "Failed","NotInstalled" -and $_.Update.Classifications.Title -match "Critical|Security" -and $_.UpdateApprovalAction -ne "Declined"} | Select-Object @{n='更新名称';e={$_.Update.Title}},@{n='状态';e={$_.InstallationState}}
自助服务门户适配说明
你可以把$targetComputerName改为参数传入,让脚本支持调用时动态指定查询的服务器名称,生成的HTML报告可以直接嵌入自助门户的iframe,也可以修改输出格式为JSON供门户接口直接调用。
内容的提问来源于stack exchange,提问作者vespavbb
相关产品推荐
相关产品推荐

