Farbix89

Forza-Windows-Update.vbs

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