Still need to add in email notification and test it headless (not logged in).. however, to solve a problem earlier today.. this will install updates that are approved (and/or already downloaded) from WSUS on our schedule. It’ll also reboot, so we can stagger systems to automatically do this on your schedule, not Microsoft’s.

 

' Automatically install approved updates from WSUS and reboot

' *** check
Set objUpdateSession = CreateObject("Microsoft.Update.Session")
Set objUpdateSearch = objUpdateSession.CreateupdateSearcher()
objUpdateSearch.ServerSelection = 0 'use system default
Set objUpdateSearchResult = objUpdateSearch.Search("IsInstalled=0 and Type='Software'")
For I = 0 To objUpdateSearchResult.Updates.Count-1
Set objUpdate = objUpdateSearchResult.Updates.Item(I)
'WScript.Echo I + 1 & "> " & objUpdate.Title
Next
If objUpdateSearchResult.Updates.Count = 0 Then WScript.Quit 'wscript.echo "no updates"
' *** download
Set objUpdatesDownload = CreateObject("Microsoft.Update.UpdateColl")
For I = 0 to objUpdateSearchResult.Updates.Count-1
Set objUpdate = objUpdateSearchResult.Updates.Item(I)
boolAddThisUpdate = false
If Not objUpdate.InstallationBehavior.CanRequestUserInput = true Then
If objUpdate.EulaAccepted = false Then
objUpdate.AcceptEula()
boolAddThisUpdate = true
Else
boolAddThisUpdate = true
end if
End If
If boolAddThisUpdate = true Then objUpdatesDownload.Add(objUpdate)
Next
If objUpdatesDownload.Count = 0 Then WScript.Quit
Set objUpdateDownloader = objUpdateSession.CreateUpdateDownloader()
objUpdateDownloader.Updates = objUpdatesDownload
objUpdateDownloader.Download()
' *** install
Set objUpdatesInstall = CreateObject("Microsoft.Update.UpdateColl")
For I = 0 To objUpdateSearchResult.Updates.Count-1
set objUpdate = objUpdateSearchResult.Updates.Item(I)
'    WScript.Echo I + 1 & "> " & objUpdate.Title
If objUpdate.IsDownloaded = true Then objUpdatesInstall.Add(objUpdate)
Next
if objUpdatesInstall.Count = 0 then wscript.quit
Set objUpdateInstaller = objUpdateSession.CreateUpdateInstaller()
objUpdateInstaller.Updates = objUpdatesInstall
Set objInstallationResult = objUpdateInstaller.Install()
' *** restart
Set objShell = CreateObject("WScript.Shell")
objShell.Run "shutdown.exe -r -t 0"
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s