Advertisement
adamchilcott

checkUpdates.ps1

Oct 8th, 2018
474
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ###########################################################
  2. # Check For Windows Updates, Then Install
  3. # Aimed At Remote Monitoring And Management Solutions (RMM)
  4. # LogMeIn Central, AVG Managed Workplace Et. al
  5. ###########################################################
  6.  
  7. ######################################################
  8. # Provide The Literal Path To Required System Binaries
  9. ######################################################
  10. $cscriptBinary = "C:\Windows\System32\cscript.exe"
  11. $scBinary = "C:\Windows\System32\sc.exe"
  12. $netBinary = "C:\Windows\System32\net.exe"
  13.  
  14. #########################
  15. # Supply checkUpdates.vbs
  16. #########################
  17. $vbsCheckUpdates = @"
  18.  
  19. 'ServerSelection values
  20. ssDefault = 0
  21. ssManagedServer   = 1
  22. ssWindowsUpdate   = 2
  23. ssOthers          = 3
  24.  
  25. 'InStr values
  26. intSearchStartChar = 1
  27.  
  28.  
  29. dim strTitle
  30.  
  31.  
  32. Set updateSession = CreateObject("Microsoft.Update.Session")
  33. Set updateSearcher = updateSession.CreateupdateSearcher()
  34.  
  35. updateSearcher.ServerSelection = ssWindowsUpdate
  36. Set searchResult = updateSearcher.Search("IsInstalled=0 and Type='Software'")
  37.  
  38. WScript.Echo "List of applicable items on the machine:"
  39.  
  40. For I = 0 To searchResult.Updates.Count-1
  41.    Set update = searchResult.Updates.Item(I)
  42.    WScript.Echo I + 1 & "> " & update.Title
  43. Next
  44.  
  45. If searchResult.Updates.Count = 0 Then
  46.    WScript.Echo "There are no applicable updates."
  47.    WScript.Quit
  48. End If
  49.  
  50. WScript.Echo vbCRLF & "Creating collection of updates to download:"
  51.  
  52. Set updatesToDownload = CreateObject("Microsoft.Update.UpdateColl")
  53.  
  54. For I = 0 to searchResult.Updates.Count-1
  55.    Set update = searchResult.Updates.Item(I)
  56.    addThisUpdate = false
  57.    If update.InstallationBehavior.CanRequestUserInput = true Then
  58.        WScript.Echo I + 1 & "> skipping: " & update.Title & _
  59.        " because it requires user input"
  60.    Else
  61.        If update.EulaAccepted = false Then
  62.            WScript.Echo I + 1 & "> note: " & update.Title & _
  63.            " has a license agreement that must be accepted:"
  64.            WScript.Echo update.EulaText
  65.            WScript.Echo "Do you accept this license agreement? (Y/N)"
  66.            ''strInput = WScript.StdIn.ReadLine
  67.            strInput = "Y"
  68.            WScript.Echo
  69.            If (strInput = "Y" or strInput = "y") Then
  70.                update.AcceptEula()
  71.                addThisUpdate = true
  72.            Else
  73.                WScript.Echo I + 1 & "> skipping: " & update.Title & _
  74.                " because the license agreement was declined"
  75.            End If
  76.        Else
  77.            addThisUpdate = true
  78.        End If
  79.    End If
  80.    If addThisUpdate = true Then
  81.        WScript.Echo I + 1 & "> adding: " & update.Title
  82.        updatesToDownload.Add(update)
  83.    End If
  84. Next
  85.  
  86. If updatesToDownload.Count = 0 Then
  87.    WScript.Echo "All applicable updates were skipped."
  88.    WScript.Quit
  89. End If
  90.    
  91. WScript.Echo vbCRLF & "Downloading updates..."
  92.  
  93. Set downloader = updateSession.CreateUpdateDownloader()
  94. downloader.Updates = updatesToDownload
  95. downloader.Download()
  96.  
  97. Set updatesToInstall = CreateObject("Microsoft.Update.UpdateColl")
  98.  
  99. rebootMayBeRequired = false
  100.  
  101. WScript.Echo vbCRLF & "Successfully downloaded updates:"
  102.  
  103. For I = 0 To searchResult.Updates.Count-1
  104.    set update = searchResult.Updates.Item(I)
  105.    If update.IsDownloaded = true Then
  106.        WScript.Echo I + 1 & "> " & update.Title
  107.         updatesToInstall.Add(update)   
  108.        If update.InstallationBehavior.RebootBehavior > 0 Then
  109.            rebootMayBeRequired = true
  110.        End If
  111.    End If
  112. Next
  113.  
  114. If updatesToInstall.Count = 0 Then
  115.    WScript.Echo "No updates were successfully downloaded."
  116.    WScript.Quit
  117. End If
  118.  
  119. If rebootMayBeRequired = true Then
  120.    WScript.Echo vbCRLF & "These updates may require a reboot."
  121. End If
  122.  
  123. WScript.Echo  vbCRLF & "Would you like to install updates now? (Y/N)"
  124. ''strInput = WScript.StdIn.ReadLine
  125. strInput = "Y"
  126. WScript.Echo
  127.  
  128. If (strInput = "Y" or strInput = "y") Then
  129.    WScript.Echo "Installing updates..."
  130.    Set installer = updateSession.CreateUpdateInstaller()
  131.    installer.Updates = updatesToInstall
  132.    Set installationResult = installer.Install()
  133.    
  134.    'Output results of install
  135.    WScript.Echo "Installation Result: " & _
  136.    installationResult.ResultCode
  137.    WScript.Echo "Reboot Required: " & _
  138.    installationResult.RebootRequired & vbCRLF
  139.    WScript.Echo "Listing of updates installed " & _
  140.    "and individual installation results:"
  141.    
  142.    For I = 0 to updatesToInstall.Count - 1
  143.        WScript.Echo I + 1 & "> " & _
  144.        updatesToInstall.Item(i).Title & _
  145.         ": " & installationResult.GetUpdateResult(i).ResultCode        
  146.    Next
  147. End If
  148.  
  149. 'ServerSelection values
  150.  
  151.  
  152. ' START NOTES
  153.  
  154. ' Reference:
  155. ' <https://gallery.technet.microsoft.com/scriptcenter/VB-Script-to-Check-and-620579cd>
  156.  
  157. ' END NOTES
  158.  
  159. "@
  160.  
  161. ###########################
  162. # Supply enableOptional.vbs
  163. ###########################
  164. $vbsEnableOptional = @"
  165.  
  166. Set ServiceManager = CreateObject("Microsoft.Update.ServiceManager")
  167. ServiceManager.ClientApplicationID = "My App"
  168.  
  169. 'add the Microsoft Update Service, GUID
  170. Set NewUpdateService = ServiceManager.AddService2("7971f918-a847-4430-9279-4a52d1efe18d",7,"")
  171.  
  172. ' START NOTES
  173.  
  174. ' Reference:
  175. ' <https://docs.microsoft.com/en-gb/windows/desktop/Wua_Sdk/opt-in-to-microsoft-update>
  176. ' <https://blogs.technet.microsoft.com/danbuche/2010/01/06/enabling-and-disabling-microsoft-update-in-windows-7-via-script/>
  177.  
  178. ' END NOTES
  179.  
  180. "@
  181.  
  182. #######################
  183. # Write Out *.vbs Files
  184. #######################
  185. $vbsCheckUpdates | Out-File checkUpdates.vbs -Force
  186. $vbsEnableOptional | Out-File enableOptional.vbs -Force
  187.  
  188. ###########################
  189. # Enable 'wuauserv' Service
  190. ###########################
  191. & $scBinary config wuauserv start= auto
  192. & $netBinary start wuauserv
  193.  
  194. ###############################
  195. # Interpret Written *.vbs Files
  196. ###############################
  197. & $cscriptBinary enableOptional.vbs
  198. & $cscriptBinary checkUpdates.vbs
  199.  
  200. ######################
  201. # Clean-Up *.vbs Files
  202. ######################
  203. Remove-Item .\enableOptional.vbs -Force
  204. Remove-Item .\checkUpdates.vbs -Force
  205.  
  206. #############
  207. # START NOTES
  208. #############
  209.  
  210. ## You Must Run This PowerShell Script As An Administrator.
  211. ## Leverages 'Here-Strings' To Output The Self-Contained VBScript(s).
  212. ## In Future, Instead Of Writing Out To A File, Perhaps VBScript(s) Can Be Piped Directly To The Interpreter.
  213.  
  214. ## Use 'C:\Windows\System32\where.exe' To Find Literal Binary Paths.
  215. ##
  216. ## OR
  217. ##
  218. ## 'Get-Command'.
  219.  
  220. ###########
  221. # END NOTES
  222. ###########
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement