I have spent a little time today troubleshooting an issue on a WSUS server that manages updates for our network. I couldn’t figure out why updates weren’t being installed on 40+ computers and wanted to test to make sure that WSUS was correctly configured as well as all the client PCs. I did end up figuring out what the problem was after all this work, and it is a little embarrassing to admit. The updates weren’t pushing to clients automatically because not all the updates were approved, and even after approving them I still had an installation issue and discovered it is because they weren’t synchronized to the local repository. *facepalm*
Anyway, here’s a VBScript code snippet that will check for updates and prompt to A) download and B) install.
I tried to document with MSDN reference links where appropriate for those of you who might stumble across this. The script in and of itself is based largely (if not almost completely) off the “Searching, Download and Installing Updates” article from Microsoft.
set updateSession = createobject("Microsoft.Update.Session")
updateSession.ClientApplicationID = "VBScript Updater"
set updateSearcher = updateSession.CreateUpdateSearcher()
updateSearcher.Online = False ' http://msdn.microsoft.com/en-us/library/windows/desktop/aa386525(v=vs.85).aspx
updateSearcher.ServerSelection = 1 ' http://msdn.microsoft.com/en-us/library/windows/desktop/aa387280(v=vs.85).aspx
wscript.echo "Searching for updates..." & vbcrlf
set searchResult = updatesearcher.Search("IsInstalled=0 and Type='Software' and IsHidden=0 and IsAssigned=1") ' http://msdn.microsoft.com/en-us/library/windows/desktop/aa386526(v=vs.85).aspx
WScript.Echo vbCRLF & "Creating collection of updates to download:"
Set updatesToDownload = CreateObject("Microsoft.Update.UpdateColl")
For I = 0 to searchResult.Updates.Count-1
Set update = searchResult.Updates.Item(I)
WScript.Echo I + 1 & "> adding: " & update.Title
updatesToDownload.Add(update)
If update.EulaAccepted = False Then
update.AcceptEula
WScript.Echo I + 1 & "> Accept EULA " & update.Title
End If
Next
If updatesToDownload.Count = 0 Then
WScript.Echo "All applicable updates were skipped."
WScript.Quit
End If
WScript.Echo vbCRLF & "Would you like to download updates now? (Y/N)"
strInput = WScript.StdIn.Readline
WScript.Echo
If (strInput = "Y" or strInput = "y") Then
WScript.Echo vbCRLF & "Downloading updates..."
Set downloader = updateSession.CreateUpdateDownloader()
downloader.Updates = updatesToDownload
downloader.Download()
WScript.Echo vbCRLF & "Successfully downloaded updates:"
Set updatesToInstall = CreateObject("Microsoft.Update.UpdateColl")
rebootMayBeRequired = false
For I = 0 To searchResult.Updates.Count-1
set update = searchResult.Updates.Item(I)
If update.IsDownloaded = true Then
WScript.Echo I + 1 & "> " & update.Title
updatesToInstall.Add(update)
If update.InstallationBehavior.RebootBehavior > 0 Then
rebootMayBeRequired = true
End If
End If
Next
If updatesToInstall.Count = 0 Then
WScript.Echo "No updates were successfully downloaded."
WScript.Quit
End If
If rebootMayBeRequired = true Then
WScript.Echo vbCRLF & "These updates may require a reboot."
End If
Else
Wscript.Quit
End If
WScript.Echo vbCRLF & "Would you like to install updates now? (Y/N)"
strInput = WScript.StdIn.Readline
WScript.Echo
If (strInput = "Y" or strInput = "y") Then
Set installer = updateSession.CreateUpdateInstaller()
If installer.IsBusy = True Then
wscript.echo "Another installation is currently in progress, try later."
wscript.quite
else
WScript.Echo "Installing updates..."
installer.Updates = updatesToInstall
Set installationResult = installer.Install()
'Output results of install
WScript.Echo "Installation Result: " & _
installationResult.ResultCode
WScript.Echo "Reboot Required: " & _
installationResult.RebootRequired & vbCRLF
WScript.Echo "Listing of updates installed " & _
"and individual installation results:"
For I = 0 to updatesToInstall.Count - 1
WScript.Echo I + 1 & "> " & _
updatesToInstall.Item(i).Title & _
": " & installationResult.GetUpdateResult(i).ResultCode ' http://msdn.microsoft.com/en-us/library/windows/desktop/aa387095(v=vs.85).aspx
Next
end if
End If








