Advertisement
adamchilcott

windowsUpdateScript.vbs

Oct 13th, 2018
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'v1.0 WindowsUpdates.vbs by Microsoft
  2. 'v1.1 Ammendment by D. Collins to increase logging
  3. 'v1.2 Ammendment by I. Atkin to introduce batching
  4. '     ~~~ With 100 pending updates to install, TrustedInstaller.exe can consume
  5. '     ~~~ >3GB RAM and impede the updates process. So here limit the number of updates
  6. '     ~~~ that can be queued and installed at any one time.
  7.  
  8.  
  9. Dim I, I2, oSession, oSearcher, oSearchResult, oUpdate, oUpdatesToDownload, oUpdatesToInstall, oDownloader, oInstaller, oInstallationResult
  10. Dim iMaxUpdates
  11. Dim oFileSystem, oLog
  12. dim oWinFolder
  13. Dim blNoUpdates, qVal, oRunLog
  14.  
  15. blNoUpdates = False
  16.  
  17.  
  18.  
  19.  
  20. iMaxUpdates=30
  21.  
  22. ' ~~~ ---------------------------------------------
  23. ' ~~~ Create objects
  24. ' ~~~ ---------------------------------------------
  25. Set oSession       = CreateObject("Microsoft.Update.Session")
  26. Set oSearcher      = oSession.CreateupdateSearcher()
  27.  
  28. Set oFileSystem = CreateObject("Scripting.FileSystemObject")
  29. Set oWinFolder = oFileSystem.GetSpecialFolder (0)
  30. Set oLog = oWinFolder.CreateTextFile("SCTWindowsUpdates.log", True)
  31. Set oRunLog = oFileSystem.OpenTextFile("C:\Logs\WindowsUpdates-All.log", 8, True)
  32.  
  33. Public Function DoWindowsUpdates
  34.  
  35. On Error Resume Next
  36.  
  37.     ' ~~~ ---------------------------------------------
  38.    ' ~~~ Search for updates
  39.    ' ~~~ ---------------------------------------------
  40.    oLog.WriteLine FormatDateTime (Now) & " Searching for updates..." & vbCRLF
  41.     oLog.WriteLine FormatDateTime (Now) & " List of applicable items on the machine:"
  42.    
  43.     I2=0
  44.     For I = 0 To oSearchResult.Updates.Count-1
  45.         Set oUpdate = oSearchResult.Updates.Item(I)
  46.         I2=I2+1
  47.         oLog.WriteLine I + 1 & "> " & oUpdate.Title
  48.         oRunLog.WriteLine Now() & " - " & I + 1 & " > " & oUpdate.Title
  49.     Next
  50.    
  51.     If I2 = 0 Then
  52.         oLog.WriteLine FormatDateTime (Now) & " There are no applicable updates."
  53.                 blNoUpdates = True
  54.         Exit Function
  55.     Else
  56.         oRunLog.WriteLine Now() & " - Applicable updates: " & I2
  57.     End If
  58.    
  59.     ' ~~~ ---------------------------------------------
  60.    ' ~~~ Create collection of upates to download
  61.    ' ~~~ ---------------------------------------------
  62.    oLog.WriteLine vbCRLF & FormatDateTime (Now) & " Creating collection of updates to download:"
  63.     Set oUpdatesToDownload = CreateObject("Microsoft.Update.UpdateColl")
  64.    
  65.         Dim iUpdatesCount
  66.         iUpdatesCount=oSearchResult.Updates.Count
  67.         if iUpdatesCount > iMaxUpdates then
  68.         iUpdatesCount=iMaxUpdates
  69.         oRunLog.WriteLine Now() & " - Too many updates. To limit trustedinstaller overhead, reducing to " & iMaxUpdates
  70.         End if
  71.  
  72.     For I = 0 to iUpdatesCount-1
  73.         Set oUpdate = oSearchResult.Updates.Item(I)
  74.         oLog.WriteLine I + 1 & "> adding: " & oUpdate.Title
  75.         oRunLog.WriteLine I + 1 & "> adding: " & oUpdate.Title
  76.         oUpdatesToDownload.Add(oUpdate)
  77.     Next
  78.    
  79.     ' ~~~ ---------------------------------------------
  80.    ' ~~~ Download updates
  81.    ' ~~~ ---------------------------------------------
  82.    oLog.WriteLine vbCRLF & FormatDateTime (Now) & " Downloading updates..."
  83.    
  84.     Set oDownloader = oSession.CreateUpdateDownloader()
  85.     oDownloader.Updates = oUpdatesToDownload
  86.     oDownloader.Download()
  87.    
  88.     ' ~~~ ---------------------------------------------
  89.    ' ~~~ Create a collection of downloaded updates to install
  90.    ' ~~~ ---------------------------------------------
  91.    oLog.WriteLine  vbCRLF & FormatDateTime (Now) & " Creating collection of downloaded updates to install:"
  92.     Set oUpdatesToInstall = CreateObject("Microsoft.Update.UpdateColl")
  93.    
  94.     For I = 0 To iUpdatesCount-1
  95.         Set oUpdate = oSearchResult.Updates.Item(I)
  96.         oLog.WriteLine I + 1 & "> adding:  " & oUpdate.Title
  97.         oUpdatesToInstall.Add(oUpdate)    
  98.     Next
  99.    
  100.     ' ~~~ ---------------------------------------------
  101.    ' ~~~ Install updates
  102.    ' ~~~ ---------------------------------------------
  103.    oLog.WriteLine FormatDateTime (Now) & " Installing updates..."
  104.     Set oInstaller = oSession.CreateUpdateInstaller()
  105.     oInstaller.Updates = oUpdatesToInstall
  106.     oInstaller.ForceQuiet = true
  107.     Set oInstallationResult = oInstaller.Install()
  108.    
  109.     ' ~~~ ---------------------------------------------
  110.    ' ~~~ Output results of install
  111.    ' ~~~ ---------------------------------------------
  112.    oLog.WriteLine FormatDateTime (Now) & " Installation Result: " & oInstallationResult.ResultCode
  113.     oLog.WriteLine FormatDateTime (Now) & " Reboot Required: " & oInstallationResult.RebootRequired & vbCRLF
  114.     oLog.WriteLine FormatDateTime (Now) & " Listing of updates installed and individual installation results:"
  115.    
  116.     For I = 0 to oUpdatesToInstall.Count - 1
  117.         oLog.WriteLine I + 1 & "> " & oUpdatesToInstall.Item(i).Title & ": " & oInstallationResult.GetUpdateResult(i).ResultCode
  118.     Next
  119.    
  120. End Function
  121.  
  122.  
  123. On Error Resume Next
  124.  
  125. oRunLog.WriteLine Now() & " - Start: Windows Updates All script starting."
  126.  
  127. oLog.WriteLine FormatDateTime (Now) & " Downloading and Installing All Available Software Updates"
  128. Set oSearchResult  = oSearcher.Search("IsAssigned=1 and IsHidden=0 and IsInstalled=0 and Type='Software'")
  129. Call DoWindowsUpdates
  130. oLog.WriteLine vbCRLF & vbCRLF
  131.  
  132. oLog.WriteLine FormatDateTime (Now) & " Updates Complete"
  133.  
  134. oLog.Close
  135.  
  136. If blNoUpdates = True Then
  137.     oRunLog.WriteLine Now() & " - No Updates Found."
  138.     qVal = 99
  139. Else
  140.     oRunLog.WriteLine Now() & " - Updates found and installed.  See %WinDir%\WindowsUpdate.log for details."
  141.     qVal = 98
  142. End If
  143.  
  144. oRunLog.WriteLine Now() & " - End: Windows Updates All script Complete."
  145. If qVal = 0 Then WScript.Quit(Err.Number)
  146. WScript.Quit(qVal)
  147.  
  148. ' START NOTES
  149.  
  150. ' Reference:
  151. ' <https://www.symantec.com/connect/downloads/windows-update-vbscript-v12>
  152.  
  153. ' END NOTES
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement