有人可以举例说明如何在PowerShell中从Windows Update获取更新历史记录吗?
我在此处找到了API文档:https://msdn.microsoft.com/en-us/library/windows/desktop/bb394842(v=vs.85).aspx
但是,如何从PowerShell调用它很少。
答案 0 :(得分:3)
以下是如何从PowerShell查询WUA历史记录的示例。首先,我将定义一些函数来帮助。
# Convert Wua History ResultCode to a Name
# 0, and 5 are not used for history
# See https://msdn.microsoft.com/en-us/library/windows/desktop/aa387095(v=vs.85).aspx
function Convert-WuaResultCodeToName
{
param(
[Parameter(Mandatory=$true)]
[int] $ResultCode
)
$Result = $ResultCode
switch($ResultCode)
{
2 {
$Result = "Succeeded"
}
3 {
$Result = "Succeeded With Errors"
}
4 {
$Result = "Failed"
}
}
return $Result
}
function Get-WuaHistory
{
# Get a WUA Session
$session = (New-Object -ComObject 'Microsoft.Update.Session')
# Query the latest 1000 History starting with the first recordp
$history = $session.QueryHistory("",0,1000) | ForEach-Object {
$Result = Convert-WuaResultCodeToName -ResultCode $_.ResultCode
# Make the properties hidden in com properties visible.
$_ | Add-Member -MemberType NoteProperty -Value $Result -Name Result
$Product = $_.Categories | Where-Object {$_.Type -eq 'Product'} | Select-Object -First 1 -ExpandProperty Name
$_ | Add-Member -MemberType NoteProperty -Value $_.UpdateIdentity.UpdateId -Name UpdateId
$_ | Add-Member -MemberType NoteProperty -Value $_.UpdateIdentity.RevisionNumber -Name RevisionNumber
$_ | Add-Member -MemberType NoteProperty -Value $Product -Name Product -PassThru
Write-Output $_
}
#Remove null records and only return the fields we want
$history |
Where-Object {![String]::IsNullOrWhiteSpace($_.title)} |
Select-Object Result, Date, Title, SupportUrl, Product, UpdateId, RevisionNumber
}
定义了这些功能后,我们可以获得更新
# Get all the update History, formatted as a table
Get-WuaHistory | Format-Table