2

我厌倦了编写一个 Powershell 脚本,它将返回可用的 Windows 更新计数,就像 Windows 返回计数一样。我遇到的问题是我无法让我的计数与 Window's Update 返回的计数相匹配。

例如我的脚本可能会返回:
关键计数:0
重要计数:1
可选计数:30

但 Windows 更新会说有:
关键计数:1
重要计数:1
可选计数:29

有谁知道 Windows 使用什么标准来显示Windows 更新中的计数?
这是我的代码示例:

# ----- Get All Assigned updates --------
$UpdateSession = New-Object -ComObject "Microsoft.Update.Session"
$UpdateSearcher = $UpdateSession.CreateUpdateSearcher()
$SearchResult = $UpdateSearcher.Search('IsInstalled=0')

# ----- Matrix Results for type of updates that are needed --------
$critical = $SearchResult.updates | where { $_.MsrcSeverity -eq "Critical" }
$important = $SearchResult.updates | where { $_.MsrcSeverity -eq "Important" }
$optional = $SearchResult.updates | where { ($_.MsrcSeverity -ne "Critical") -and ($_.MsrcSeverity -ne "Important") }


4

2 回答 2

3

尝试运行它,不确定它是否能解决您的问题。我没有 powershell 可访问的 atm。

#Get All Assigned updates in $SearchResult
$UpdateSession = New-Object -ComObject Microsoft.Update.Session
$UpdateSearcher = $UpdateSession.CreateUpdateSearcher()
$SearchResult = $UpdateSearcher.Search("IsAssigned=1 and IsHidden=0 and IsInstalled=0")


#Matrix Results for type of updates that are needed
$Critical = $SearchResult.updates | where { $_.MsrcSeverity -eq "Critical" }
$important = $SearchResult.updates | where { $_.MsrcSeverity -eq "Important" }
$other = $SearchResult.updates | where { $_.MsrcSeverity -eq $null }

#Write Results
Write-Host "total=$($SearchResult.updates.count)"
Write-Host "critical=$($Critical.count)"
Write-Host "important=$($Important.count)"
Write-Host "other=$($other.count)"
于 2012-03-09T19:42:01.567 回答
1

这是一篇旧帖子,但我将分享我在解决此问题时发现的内容,希望其他人可能会发现它有用。

此行为是当从 WHERE 子句返回单个对象时 PowerShell 未返回数组/集合的结果。在下面的屏幕截图中,我使用类似的代码进行了测试,并按每个 MsrcSeverity 级别对其进行了分解。未分类的严重性有一个更新,但当我尝试获取计数时 PowerShell 返回了一个空值。

您还可以看到,通过调用 $uncategorizedupdates 变量,我看到的是实际更新而不是集合。

输出截图

要解决这个问题,我们所要做的就是将我们的变量显式定义为一个 [array],然后即使只有一个对象存在,它也会返回一个对象集合。只需修改您的代码,如下所示....

[array]$critical = $SearchResult.updates | where { $_.MsrcSeverity -eq "Critical" }
[array]$important = $SearchResult.updates | where { $_.MsrcSeverity -eq "Important" }
[array]$optional = $SearchResult.updates | where { ($_.MsrcSeverity -ne "Critical") -and ($_.MsrcSeverity -ne "Important") }

我也在 Windows 10 上复制了这个,所以即使在 PowerShell 5 中它仍然是一个问题。希望这会有所帮助。

于 2017-10-23T16:53:54.407 回答