|
| 1 | +<!-- : |
| 2 | +if NOT "%UPDATE%" == "true" exit |
| 3 | +echo ==^> Running Windows Update |
| 4 | +cscript //nologo "%~f0?.wsf" |
| 5 | + |
| 6 | +exit /b |
| 7 | + |
| 8 | +----- Begin wsf script ---> |
| 9 | +<job><script language="VBScript"> |
| 10 | + Set updateSession = CreateObject("Microsoft.Update.Session") |
| 11 | + updateSession.ClientApplicationID = "Packer" |
| 12 | + |
| 13 | + Set updateSearcher = updateSession.CreateUpdateSearcher() |
| 14 | + WScript.Echo "Searching for updates..." & vbCRLF |
| 15 | + |
| 16 | + Set searchResult = _ |
| 17 | + updateSearcher.Search("IsInstalled=0 and Type='Software' and IsHidden=0") |
| 18 | + |
| 19 | + WScript.Echo "List of applicable items on the machine:" |
| 20 | + For I = 0 To SearchResult.Updates.Count-1 |
| 21 | + Set update = searchResult.Updates.Item(I) |
| 22 | + WScript.Echo I + 1 & "> " & update.Title |
| 23 | + Next |
| 24 | + If searchResult.Updates.Count = 0 Then |
| 25 | + WScript.Echo "There are no applicable updates." |
| 26 | + WScript.Quit |
| 27 | + End If |
| 28 | + |
| 29 | + WScript.Echo vbCRLF & "Creating collection of updates to download:" |
| 30 | + Set updatesToDownload = CreateObject("Microsoft.Update.UpdateColl") |
| 31 | + For I = 0 to searchResult.Updates.Count-1 |
| 32 | + Set update = searchResult.Updates.Item(I) |
| 33 | + addThisUpdate = false |
| 34 | + If update.InstallationBehavior.CanRequestUserInput = true Then |
| 35 | + WScript.Echo I + 1 & "> skipping: " & update.Title & _ |
| 36 | + " because it requires user input" |
| 37 | + Else |
| 38 | + If update.EulaAccepted = false Then |
| 39 | + update.AcceptEula() |
| 40 | + addThisUpdate = true |
| 41 | + Else |
| 42 | + addThisUpdate = true |
| 43 | + End If |
| 44 | + End If |
| 45 | + If addThisUpdate = true Then |
| 46 | + WScript.Echo I + 1 & "> adding: " & update.Title |
| 47 | + updatesToDownload.Add(update) |
| 48 | + End If |
| 49 | + Next |
| 50 | + |
| 51 | + If updatesToDownload.Count = 0 Then |
| 52 | + WScript.Echo "All applicable updates were skipped." |
| 53 | + WScript.Quit |
| 54 | + End If |
| 55 | + |
| 56 | + WScript.Echo vbCRLF & "Downloading updates..." |
| 57 | + |
| 58 | + |
| 59 | + Set downloader = updateSession.CreateUpdateDownloader() |
| 60 | + downloader.Updates = updatesToDownload |
| 61 | + downloader.Download() |
| 62 | + |
| 63 | + Set updatesToInstall = CreateObject("Microsoft.Update.UpdateColl") |
| 64 | + rebootMayBeRequired = false |
| 65 | + WScript.Echo vbCRLF & "Successfully downloaded updates:" |
| 66 | + |
| 67 | + For I = 0 To searchResult.Updates.Count-1 |
| 68 | + set update = searchResult.Updates.Item(I) |
| 69 | + If update.IsDownloaded = true Then |
| 70 | + WScript.Echo I + 1 & "> " & update.Title |
| 71 | + updatesToInstall.Add(update) |
| 72 | + If update.InstallationBehavior.RebootBehavior > 0 Then |
| 73 | + rebootMayBeRequired = true |
| 74 | + End If |
| 75 | + End If |
| 76 | + Next |
| 77 | + |
| 78 | + If updatesToInstall.Count = 0 Then |
| 79 | + WScript.Echo "No updates were sucessfully downloaded." |
| 80 | + WScript.Quit |
| 81 | + End If |
| 82 | + |
| 83 | + WScript.Echo "Installing updates..." |
| 84 | + Set installer = updateSession.CreateUpdateInstaller() |
| 85 | + installer.Updates = updatesToInstall |
| 86 | + Set installationResult = installer.Install() |
| 87 | + |
| 88 | + WScript.Echo "Installation Result: " & _ |
| 89 | + installationResult.ResultCode |
| 90 | + WScript.Echo "Reboot Required: " & _ |
| 91 | + installationResult.RebootRequired & vbCRLF |
| 92 | + WScript.Echo "Listing of updates installed " & _ |
| 93 | + "and individual installation results:" |
| 94 | + |
| 95 | + For I = 0 to updatesToInstall.Count - 1 |
| 96 | + WScript.Echo I + 1 & "> " & _ |
| 97 | + updatesToInstall.Item(I).Title & _ |
| 98 | + ": " & installationResult.GetUpdateResult(I).ResultCode |
| 99 | + Next |
| 100 | + |
| 101 | + If rebootMayBeRequired Then |
| 102 | + Set objShell = WScript.CreateObject("wscript.shell") |
| 103 | + objShell.Run "shutdown.exe /r /t 00" |
| 104 | + WScript.Sleep 60 |
| 105 | + End If |
| 106 | +</script></job> |
0 commit comments