如何测试 Windows 服务器是否完全更新? (尝试创建一个 update/reboot 循环脚本)

How to test if Windows Server is fully updated? (Trying to create an update/reboot loop script)

虽然我通常使用 Windows 服务器的预烘焙图像,但我偶尔会 运行 遇到这样一种情况,我必须从头开始设置一个服务器,并经历极其繁琐的检查更新过程,安装它们然后重新启动。很多很多很多次。

我正在尝试编写一个简单的脚本来自动执行此操作。

检查和安装更新很简单:

wuauclt.exe /detectnow /updatenow

重启也同样简单:

shutdown /r /t 0

但我想做的是创建一个 PowerShell 工作流,在重新启动后继续 运行ning,运行循环上述命令。

我没搞清楚的地方是:

使用 update searcher 检查挂起的更新:

$criteria = "Type='software' and IsAssigned=1 and IsHidden=0 and IsInstalled=0"

$searcher = (New-Object -COM Microsoft.Update.Session).CreateUpdateSearcher()
$updates  = $searcher.Search($criteria).Updates

if ($updates.Count -ne 0) {
  # $updates pending
} else {
  # system up-to-date
}