2

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).
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
ZZ9
  • 2,177
  • 2
  • 27
  • 38

1 Answers1

4

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
}
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • 1
    Dude... it's like you know everything PowerShell. – Matt Jun 22 '15 at 00:30
  • Not even remotely. Update management just falls into my area of expertise, since I'm working as a sysadmin. – Ansgar Wiechers Jun 22 '15 at 07:14
  • Thanks! Is there any way to check if Windows update has finished and has triggered the 'restart to continue installing updates'. Thanks – ZZ9 Jun 22 '15 at 12:02
  • @AirCombat Check for the presence of the registry key [`[HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired]`](http://blogs.msdn.com/b/hansr/archive/2006/02/17/patchreboot.aspx). – Ansgar Wiechers Jun 22 '15 at 13:30