Install Microsoft and Windows Updates

A lot of scripts out there only install windows updates, this one gets the lot.
I’ve found that there are a number of patches not included in the LanGuard patch management, so here’s a script to fill in the gaps.

This script is a modified version of the one found here – blogs.technet.com/b/chrad/archive/2009/07/13/dynamic-provisioning-with-vmm-proxy-windows-updates-and-scripts.aspx

Also just added the ability to skip updates.
So if you don’t want the bing bar or windows search or any other update, just put add the name to the array. (Should also work with Most KB Numbers)

Const ssDefault = 0
 Const ssManagedServer = 1 
 Const ssOthers = 3 'Microsoft Updates
 Const ssWindowsUpdate = 2 'Windows Updates
 'Array of updates to skip. 
 SkipUpdates = Array("bing","windows live","windows search")
 '#############################
 ' Create Session
 '#############################
 Set UpdateSession = CreateObject("Microsoft.Update.Session")
 Set UpdateSearcher = UpdateSession.CreateUpdateSearcher()
 '#############################
 ' Register Microsoft Update
 '#############################
 RegisterMu
 updateSearcher.ServerSelection = ssOthers '<==== **** CHANGE THIS TO YOUR PREFERENCE ****
 updateSearcher.ServiceID = "7971f918-a847-4430-9279-4a52d1efe18d"
 '#############################
 ' Create List of Updates to Download
 '#############################
 Set UpdatesToDownload = CreateObject("Microsoft.Update.UpdateColl")
 '#############################
 ' Search for Updates
 '#############################
 Set SearchResult = UpdateSearcher.Search("Isinstalled=0")
 '#############################
 ' Quit if No Updates Found
 '#############################
 If SearchResult.Updates.Count = 0 Then
     WScript.Echo "No updates found."
 WScript.Quit
 End If
 Dim strSpacer
 For I = 0 To SearchResult.Updates.Count-1
     Set Update = SearchResult.Updates.Item(I)
     
     ' formatting helper
     If I < 10 Then
         strSpacer = " "
     Else
         strSpacer = ""
     End If
     
     'Check updates to skip
 bSkip = false
 For Each Name in SkipUpdates
 If Instr(LCase(update.Title), LCase(Name)) > 0 Then
 bSkip = True
 Exit For
 End if
 Next
 If bSkip = True Then
 WScript.Echo "Skipping update " & update.Title
 Else
 'WScript.Echo "[" & strSpacer & I & "]  Found Update, Marking For Download:  " & update.Title
 UpdatesToDownload.Add(Update)
 End if 
     
 Next
 '#############################
 ' Download Updates
 '#############################
 Set Downloader = UpdateSession.CreateUpdateDownloader()
 Downloader.Updates = UpdatesToDownload
 On Error Resume Next
 Downloader.Download()
 If Err.number <> 0 Then
     Wscript.Echo "An error occurred in  Downloader.Download() of updates"
     Wscript.Echo "Number: " & err.number
     Wscript.Echo "Description:  " & err.Description
     Wscript.Quit (Err.number)
 End If
 On Error Goto 0
 '#############################
 ' Create List of Updates to Install
 '#############################
 Set UpdatesToInstall = CreateObject("Microsoft.Update.UpdateColl")
 For I = 0 To SearchResult.Updates.Count-1
     set Update = SearchResult.Updates.Item(I)
     If Update.IsDownloaded = true Then
         UpdatesToInstall.Add(Update)
     End If
 Next
 '#############################
 ' Install Updates
 '#############################
 Set Installer = UpdateSession.CreateUpdateInstaller()
 Installer.Updates = UpdatesToInstall
 Set InstallationResult = Installer.Install()
 wscript.Echo "Installation Result: " & InstallationResult.ResultCode
 wscript.Echo "Reboot Required: " & InstallationResult.RebootRequired
 wscript.Echo "Listing of updates installed and individual installation results:"
     
     For I = 0 to UpdatesToInstall.Count - 1
         WScript.Echo I + 1 & "> " & Chr(9) & UpdatesToInstall.Item(I).Title & Chr(9) & ": " & TranslateMuCode(InstallationResult.GetUpdateResult(i).ResultCode)
     Next
 '#############################
 ' Quit
 '#############################
 WScript.Quit
 '================================================================================
 ' Translate Microsoft Update Installation Results
 '================================================================================
 Function TranslateMuCode(theCode)
   TranslateMuCode = "[" & theCode & "] "
   if (theCode = 0) Then TranslateMuCode = TranslateMuCode & "Not Started"
   if (theCode = 1) Then TranslateMuCode = TranslateMuCode & "In Progress"
   if (theCode = 2) Then TranslateMuCode = TranslateMuCode & "Succeeded"
   if (theCode = 3) Then TranslateMuCode = TranslateMuCode & "Succeeded with Errors"
   if (theCode = 4) Then TranslateMuCode = TranslateMuCode & "Failed"
   if (theCode = 5) Then TranslateMuCode = TranslateMuCode & "Aborted"
 End Function
 '================================================================================
 ' Register Microsoft Update (if never registered)
 '================================================================================
 Function RegisterMu
     Dim fso
     Dim file
     Dim WshShell
     Dim updateService
     Dim updateServiceManager
     
     found = false
     
     Set fso = CreateObject("Scripting.FileSystemObject")    
     Set WshShell = WScript.CreateObject ("WScript.Shell")
     
     Set updateServiceManager = CreateObject("Microsoft.Update.ServiceManager")
     Set updateService = updateServiceManager.Services
     
     If err <> 0 Then
         WScript.Echo "CreateObject(Microsoft.Update.ServiceManager) failed with error 0x" & Hex(err.Number)  & err.Description
         WScript.Quit(2)
     End If
     For I=0 to updateService.Count - 1
         Set item = updateService.Item(i)
         If item.ServiceID = "7971f918-a847-4430-9279-4a52d1efe18d" Then
             found = true
         End IF
     Next    
       
     IF found = false Then
         updateServiceManager.AddService2 "7971f918-a847-4430-9279-4a52d1efe18d", 2, ""
         
         If err <> 0 Then
             WScript.Echo "updateServiceManager.AddService() failed with error 0x" & Hex(err.Number) & err.Description
         Else
             WScript.Echo "MU is registered with WU Agent"
         End IF
     End IF
 END Function
 

3 Comments

  • What is the difference in this script “Install Microsoft and Windows Updates” and your other script “PatchMyPC Script”? do I need both or will one do and if so, which is the best?

    • Hey Tim,

      PatchMyPC will only do windows updates.
      If you want Microsoft updates then the other script is the way to go.
      I will get in touch with the developer and see if he will integrate the script into PatchMyPC as that would be great.

  • Great script. I have one question. The SkipUpdates Array section works great for like Bing and Microsoft Security Essentials however despite any combination for Internet Explorer 11 for Windows 7, the script continues to installs it even though it is displayed as a skipped item in command prompt.

    I’ve used Internet Explorer, Internet Explorer 11 for Windows 7 and even the KB article but it continues to install it.

    Any ideas?

Leave a Reply

Your email address will not be published.