Guest User

Force Complete Windows Update

a guest
Jun 14th, 2013
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ' Written in 2007 by Harry Johnston, University of Waikato, New Zealand.
  2. ' This code has been placed in the public domain.  It may be freely
  3. ' used, modified, and distributed.  However it is provided with no
  4. ' warranty, either express or implied.
  5. '
  6. ' Exit Codes:
  7. '   0 = scripting failure
  8. '   1 = error obtaining or installing updates
  9. '   2 = installation successful, no further updates to install
  10. '   3 = reboot needed; rerun script after reboot
  11. '
  12. ' Note that exit code 0 has to indicate failure because that is what
  13. ' is returned if a scripting error is raised.
  14. '
  15.  
  16. Set updateSession = CreateObject("Microsoft.Update.Session")
  17.  
  18. Set updateSearcher = updateSession.CreateUpdateSearcher()
  19. Set updateDownloader = updateSession.CreateUpdateDownloader()
  20. Set updateInstaller = updateSession.CreateUpdateInstaller()
  21.  
  22. Do
  23.  
  24.   WScript.Echo
  25.   WScript.Echo "Searching for approved updates ..."
  26.   WScript.Echo
  27.  
  28.   Set updateSearch = updateSearcher.Search("IsInstalled=0")
  29.  
  30.   If updateSearch.ResultCode <> 2 Then
  31.  
  32.     WScript.Echo "Search failed with result code", updateSearch.ResultCode
  33.     WScript.Quit 1
  34.  
  35.   End If
  36.  
  37.   If updateSearch.Updates.Count = 0 Then
  38.  
  39.     WScript.Echo "There are no updates to install."
  40.     WScript.Quit 2
  41.  
  42.   End If
  43.  
  44.   Set updateList = updateSearch.Updates
  45.  
  46.   For I = 0 to updateSearch.Updates.Count - 1
  47.  
  48.     Set update = updateList.Item(I)
  49.  
  50.     WScript.Echo "Update found:", update.Title
  51.  
  52.   Next
  53.  
  54.   WScript.Echo
  55.  
  56.   updateDownloader.Updates = updateList
  57.   updateDownloader.Priority = 3
  58.  
  59.   Set downloadResult = updateDownloader.Download()
  60.  
  61.   If downloadResult.ResultCode <> 2 Then
  62.  
  63.     WScript.Echo "Download failed with result code", downloadResult.ResultCode
  64.     WScript.Echo
  65.  
  66.     WScript.Quit 1
  67.  
  68.   End If
  69.  
  70.   WScript.Echo "Download complete.  Installing updates ..."
  71.   WScript.Echo
  72.  
  73.   updateInstaller.Updates = updateList
  74.  
  75.   Set installationResult = updateInstaller.Install()
  76.  
  77.   If installationResult.ResultCode <> 2 Then
  78.  
  79.     WScript.Echo "Installation failed with result code", installationResult.ResultCode
  80.  
  81.     For I = 0 to updateList.Count - 1
  82.  
  83.       Set updateInstallationResult = installationResult.GetUpdateResult(I)
  84.       WScript.Echo "Result for " & updateList.Item(I).Title & " is " & installationResult.GetUpdateResult(I).ResultCode
  85.  
  86.     Next
  87.  
  88.     WScript.Quit 1
  89.  
  90.   End If
  91.  
  92.   If installationResult.RebootRequired Then
  93.  
  94.     WScript.Echo "The system must be rebooted to complete installation."
  95.  
  96.     WScript.Quit 3
  97.  
  98.   End If
  99.  
  100.   WScript.Echo "Installation complete."
  101.  
  102. Loop
Add Comment
Please, Sign In to add comment