如何用PowerShell获取并安装处于Pending install状态的KB更新
解决Pending Install状态更新的PowerShell检测与安装问题
先尝试PSWindowsUpdate模块的原生参数
PSWindowsUpdate的Get-WUList默认不会展示已下载待安装的更新,你可以添加-Downloaded参数筛选这类Pending Install状态的更新:
# 列出已下载待安装的更新 Get-WUList -Downloaded
如果能查到目标更新,直接用Install-WindowsUpdate完成安装,可加上-AcceptAll自动接受许可,-AutoReboot安装后自动重启:
Get-WUList -Downloaded | Install-WindowsUpdate -AcceptAll -AutoReboot
用Windows Update COM API兜底查询
如果上述方法仍无法检测到,说明这类更新不在PSWindowsUpdate的默认搜索范围内,可直接调用Windows原生的Update COM对象查询:
# 初始化Windows Update会话 $updateSession = New-Object -ComObject Microsoft.Update.Session $updateSearcher = $updateSession.CreateUpdateSearcher() # 搜索已下载但未安装的更新(匹配Pending Install状态) $searchResult = $updateSearcher.Search("IsInstalled=0 and IsDownloaded=1") # 输出更新详情 if ($searchResult.Updates.Count -gt 0) { $searchResult.Updates | ForEach-Object { Write-Host "KB编号: $($_.KBArticleIDs -join ', ')" Write-Host "更新标题: $($_.Title)" Write-Host "-------------------------" } } else { Write-Host "未找到处于Pending Install状态的更新" }
安装找到的Pending更新
如果查到了目标更新,用以下代码执行安装:
if ($searchResult.Updates.Count -gt 0) { $updateCollection = New-Object -ComObject Microsoft.Update.UpdateColl $searchResult.Updates | ForEach-Object { $updateCollection.Add($_) | Out-Null } $installer = $updateSession.CreateUpdateInstaller() $installer.Updates = $updateCollection # 执行安装并返回结果 $installResult = $installer.Install() switch ($installResult.ResultCode) { 0 { Write-Host "所有更新安装成功" } 1 { Write-Host "安装成功,系统需要重启以完成" } 2 { Write-Host "部分或全部更新安装失败" } default { Write-Host "安装结果代码: $($installResult.ResultCode)" } } }
排查Pending Restart的情况
有时UI显示的"Pending Install"实际是已安装完成但等待重启的更新,可通过以下方式确认:
# 用PSWindowsUpdate模块检查重启需求 Get-WURebootStatus # 或用原生COM对象查询 $updateSearcher = New-Object -ComObject Microsoft.Update.Session | Select-Object -ExpandProperty CreateUpdateSearcher $historyCount = $updateSearcher.GetTotalHistoryCount() $rebootRequired = $updateSearcher.QueryHistory(0, $historyCount) | Where-Object { $_.RebootRequired } if ($rebootRequired) { Write-Host "系统需重启以完成更新,执行重启请运行:Restart-Computer -Force" }
内容的提问来源于stack exchange,提问作者Paul
相关产品推荐
相关产品推荐

