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

7.1k views Asked by At

Although I generally use pre-baked images of Windows Servers I occaisionally run into a situation where I have to set one up from scratch and go through the incredibly tedious process of checking for updates, installing them and then rebooting. Many, many, many times.

I am trying to write a simple script to automate this.

The checking and installing updates is straightforward:

wuauclt.exe /detectnow /updatenow

And the rebooting is just as straightforward:

shutdown /r /t 0

But what I would like to do is create a PowerShell workflow that continues running after reboot, running the above commands in a loop.

The areas I have not figured out are:

  • How to check for whether the updates have completed.
  • How to test for no remaining updates available to install (AKA Windows is fully updated and the script can stop).
1

There are 1 answers

4
Ansgar Wiechers On BEST ANSWER

Use an update searcher to check for pending updates:

$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
}