Advertisement
adamchilcott

checkUpdates.vbs

Oct 8th, 2018
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'ServerSelection values
  2. ssDefault = 0
  3. ssManagedServer   = 1
  4. ssWindowsUpdate   = 2
  5. ssOthers          = 3
  6.  
  7. 'InStr values
  8. intSearchStartChar = 1
  9.  
  10.  
  11. dim strTitle
  12.  
  13.  
  14. Set updateSession = CreateObject("Microsoft.Update.Session")
  15. Set updateSearcher = updateSession.CreateupdateSearcher()
  16.  
  17. updateSearcher.ServerSelection = ssWindowsUpdate
  18. Set searchResult = updateSearcher.Search("IsInstalled=0 and Type='Software'")
  19.  
  20. WScript.Echo "List of applicable items on the machine:"
  21.  
  22. For I = 0 To searchResult.Updates.Count-1
  23.     Set update = searchResult.Updates.Item(I)
  24.     WScript.Echo I + 1 & "> " & update.Title
  25. Next
  26.  
  27. If searchResult.Updates.Count = 0 Then
  28.     WScript.Echo "There are no applicable updates."
  29.     WScript.Quit
  30. End If
  31.  
  32. WScript.Echo vbCRLF & "Creating collection of updates to download:"
  33.  
  34. Set updatesToDownload = CreateObject("Microsoft.Update.UpdateColl")
  35.  
  36. For I = 0 to searchResult.Updates.Count-1
  37.     Set update = searchResult.Updates.Item(I)
  38.     addThisUpdate = false
  39.     If update.InstallationBehavior.CanRequestUserInput = true Then
  40.         WScript.Echo I + 1 & "> skipping: " & update.Title & _
  41.         " because it requires user input"
  42.     Else
  43.         If update.EulaAccepted = false Then
  44.             WScript.Echo I + 1 & "> note: " & update.Title & _
  45.             " has a license agreement that must be accepted:"
  46.             WScript.Echo update.EulaText
  47.             WScript.Echo "Do you accept this license agreement? (Y/N)"
  48.             ''strInput = WScript.StdIn.ReadLine
  49.            strInput = "Y"
  50.             WScript.Echo
  51.             If (strInput = "Y" or strInput = "y") Then
  52.                 update.AcceptEula()
  53.                 addThisUpdate = true
  54.             Else
  55.                 WScript.Echo I + 1 & "> skipping: " & update.Title & _
  56.                 " because the license agreement was declined"
  57.             End If
  58.         Else
  59.             addThisUpdate = true
  60.         End If
  61.     End If
  62.     If addThisUpdate = true Then
  63.         WScript.Echo I + 1 & "> adding: " & update.Title
  64.         updatesToDownload.Add(update)
  65.     End If
  66. Next
  67.  
  68. If updatesToDownload.Count = 0 Then
  69.     WScript.Echo "All applicable updates were skipped."
  70.     WScript.Quit
  71. End If
  72.    
  73. WScript.Echo vbCRLF & "Downloading updates..."
  74.  
  75. Set downloader = updateSession.CreateUpdateDownloader()
  76. downloader.Updates = updatesToDownload
  77. downloader.Download()
  78.  
  79. Set updatesToInstall = CreateObject("Microsoft.Update.UpdateColl")
  80.  
  81. rebootMayBeRequired = false
  82.  
  83. WScript.Echo vbCRLF & "Successfully downloaded updates:"
  84.  
  85. For I = 0 To searchResult.Updates.Count-1
  86.     set update = searchResult.Updates.Item(I)
  87.     If update.IsDownloaded = true Then
  88.         WScript.Echo I + 1 & "> " & update.Title
  89.         updatesToInstall.Add(update)   
  90.         If update.InstallationBehavior.RebootBehavior > 0 Then
  91.             rebootMayBeRequired = true
  92.         End If
  93.     End If
  94. Next
  95.  
  96. If updatesToInstall.Count = 0 Then
  97.     WScript.Echo "No updates were successfully downloaded."
  98.     WScript.Quit
  99. End If
  100.  
  101. If rebootMayBeRequired = true Then
  102.     WScript.Echo vbCRLF & "These updates may require a reboot."
  103. End If
  104.  
  105. WScript.Echo  vbCRLF & "Would you like to install updates now? (Y/N)"
  106. ''strInput = WScript.StdIn.ReadLine
  107. strInput = "Y"
  108. WScript.Echo
  109.  
  110. If (strInput = "Y" or strInput = "y") Then
  111.     WScript.Echo "Installing updates..."
  112.     Set installer = updateSession.CreateUpdateInstaller()
  113.     installer.Updates = updatesToInstall
  114.     Set installationResult = installer.Install()
  115.    
  116.     'Output results of install
  117.    WScript.Echo "Installation Result: " & _
  118.     installationResult.ResultCode
  119.     WScript.Echo "Reboot Required: " & _
  120.     installationResult.RebootRequired & vbCRLF
  121.     WScript.Echo "Listing of updates installed " & _
  122.     "and individual installation results:"
  123.    
  124.     For I = 0 to updatesToInstall.Count - 1
  125.         WScript.Echo I + 1 & "> " & _
  126.         updatesToInstall.Item(i).Title & _
  127.         ": " & installationResult.GetUpdateResult(i).ResultCode        
  128.     Next
  129. End If
  130.  
  131. 'ServerSelection values
  132.  
  133.  
  134. ' START NOTES
  135.  
  136. ' Reference:
  137. ' <https://gallery.technet.microsoft.com/scriptcenter/VB-Script-to-Check-and-620579cd>
  138.  
  139. ' END NOTES
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement