Compare commits

...

3 Commits

Author SHA1 Message Date
Fierelier c6adbee367 Convert line endings back to Windows' for compatibility 2021-12-26 04:42:13 +01:00
Fierelier 9660ef45b2 Show results as text, get rid of weird new lines 2021-12-26 04:41:34 +01:00
Fierelier 5e47b6484b Fix broken code 2021-12-26 04:41:05 +01:00
1 changed files with 118 additions and 110 deletions

View File

@ -1,111 +1,119 @@
Set userUpdatesToApply = CreateObject("System.Collections.ArrayList")
Set updateSession = CreateObject("Microsoft.Update.Session")
'updateSession.ClientApplicationID = "MSDN Sample Script"
Set updateSearcher = updateSession.CreateUpdateSearcher()
WScript.Echo "Searching for updates..." & vbCRLF
Set searchResult = _
Set searchString = "IsInstalled=0 and Type='Software' and IsHidden=0"
' Hide optional updates:
Set searchString = searchString & " and BrowseOnly=0"
updateSearcher.Search(searchString)
If searchResult.Updates.Count = 0 Then
WScript.Echo "There are no applicable updates."
WScript.Quit
End If
Do While True
WScript.Echo "List of applicable items on the machine:"
For I = 0 To searchResult.Updates.Count-1
Set update = searchResult.Updates.Item(I)
WScript.Echo "> " & I & ": " & update.Title
Next
WScript.Echo ""
WScript.StdOut.Write "List of items to apply on the machine: "
For I = 0 to userUpdatesToApply.Count-1
WScript.StdOut.Write userUpdatesToApply.Item(I) & ", "
Next
WScript.Echo ""
WScript.Echo ""
WScript.Echo "Enter a number and press RETURN to add an update to the queue."
WScript.Echo "Enter OK (capitalized) and press RETURN to start updating."
userInputUpdate = WScript.StdIn.Readline
If userInputUpdate = "OK" Then
Exit Do
End If
userUpdatesToApply.Add CInt(userInputUpdate)
WScript.Echo ""
WScript.Echo ""
Loop
WScript.Echo ""
WScript.Echo vbCRLF & "Creating collection of updates to download:"
Set updatesToDownload = CreateObject("Microsoft.Update.UpdateColl")
For I = 0 to userUpdatesToApply.Count-1
Set update = searchResult.Updates.Item(userUpdatesToApply.Item(I))
addThisUpdate = false
If update.EulaAccepted = false Then
update.AcceptEula()
End If
addThisUpdate = true
If addThisUpdate = true Then
WScript.Echo I + 1 & "> adding: " & update.Title
updatesToDownload.Add(update)
End If
Next
If updatesToDownload.Count = 0 Then
WScript.Echo "All applicable updates were skipped."
WScript.Quit
End If
WScript.Echo vbCRLF & "Downloading updates..."
Set downloader = updateSession.CreateUpdateDownloader()
downloader.Updates = updatesToDownload
downloader.Download()
Set updatesToInstall = CreateObject("Microsoft.Update.UpdateColl")
rebootMayBeRequired = false
WScript.Echo vbCRLF & "Successfully downloaded updates:"
For I = 0 To userUpdatesToApply.Count-1
set update = searchResult.Updates.Item(userUpdatesToApply.Item(I))
If update.IsDownloaded = true Then
WScript.Echo I + 1 & "> " & update.Title
updatesToInstall.Add(update)
If update.InstallationBehavior.RebootBehavior > 0 Then
rebootMayBeRequired = true
End If
End If
Next
If updatesToInstall.Count = 0 Then
WScript.Echo "No updates were successfully downloaded."
WScript.Quit
End If
If rebootMayBeRequired = true Then
WScript.Echo vbCRLF & "These updates may require a reboot."
End If
WScript.Echo ""
WScript.Echo "Installing updates..."
Set installer = updateSession.CreateUpdateInstaller()
installer.Updates = updatesToInstall
Set installationResult = installer.Install()
'Output results of install
WScript.Echo "Installation Result: " & _
installationResult.ResultCode
WScript.Echo "Reboot Required: " & _
installationResult.RebootRequired & vbCRLF
WScript.Echo "Listing of updates installed " & _
"and individual installation results:"
For I = 0 to updatesToInstall.Count - 1
WScript.Echo I + 1 & "> " & _
updatesToInstall.Item(i).Title & _
": " & installationResult.GetUpdateResult(i).ResultCode
Set userUpdatesToApply = CreateObject("System.Collections.ArrayList")
Function InstallationResultToText(result)
Select Case result
Case 2
InstallationResultToText = "Succeeded"
Case 3
InstallationResultToText = "Succeeded with errors"
Case 4
InstallationResultToText = "Failed"
Case 5
InstallationResultToText = "Cancelled"
Case Else
InstallationResultToText = "Unexpected (" & result & ")"
End Select
End Function
Set updateSession = CreateObject("Microsoft.Update.Session")
'updateSession.ClientApplicationID = "MSDN Sample Script"
Set updateSearcher = updateSession.CreateUpdateSearcher()
WScript.Echo "Searching for updates..." & vbCRLF
searchString = "IsInstalled=0 and Type='Software' and IsHidden=0"
' Hide optional updates:
searchString = searchString & " and BrowseOnly=0"
Set searchResult = updateSearcher.Search(searchString)
If searchResult.Updates.Count = 0 Then
WScript.Echo "There are no applicable updates."
WScript.Quit
End If
Do While True
WScript.Echo "List of applicable items on the machine:"
For I = 0 To searchResult.Updates.Count-1
Set update = searchResult.Updates.Item(I)
WScript.Echo "> " & I & ": " & update.Title
Next
WScript.Echo ""
WScript.StdOut.Write "List of items to apply on the machine: "
For I = 0 to userUpdatesToApply.Count-1
WScript.StdOut.Write userUpdatesToApply.Item(I) & ", "
Next
WScript.Echo ""
WScript.Echo ""
WScript.Echo "Enter a number and press RETURN to add an update to the queue."
WScript.Echo "Enter OK (capitalized) and press RETURN to start updating."
userInputUpdate = WScript.StdIn.Readline
If userInputUpdate = "OK" Then
Exit Do
End If
userUpdatesToApply.Add CInt(userInputUpdate)
WScript.Echo ""
WScript.Echo ""
Loop
WScript.Echo ""
WScript.Echo vbCRLF & "Creating collection of updates to download:"
Set updatesToDownload = CreateObject("Microsoft.Update.UpdateColl")
For I = 0 to userUpdatesToApply.Count-1
Set update = searchResult.Updates.Item(userUpdatesToApply.Item(I))
addThisUpdate = false
If update.EulaAccepted = false Then
update.AcceptEula()
End If
addThisUpdate = true
If addThisUpdate = true Then
WScript.Echo I + 1 & "> adding: " & update.Title
updatesToDownload.Add(update)
End If
Next
If updatesToDownload.Count = 0 Then
WScript.Echo "All applicable updates were skipped."
WScript.Quit
End If
WScript.Echo vbCRLF & "Downloading updates..."
Set downloader = updateSession.CreateUpdateDownloader()
downloader.Updates = updatesToDownload
downloader.Download()
Set updatesToInstall = CreateObject("Microsoft.Update.UpdateColl")
rebootMayBeRequired = false
WScript.Echo vbCRLF & "Successfully downloaded updates:"
For I = 0 To userUpdatesToApply.Count-1
set update = searchResult.Updates.Item(userUpdatesToApply.Item(I))
If update.IsDownloaded = true Then
WScript.Echo I + 1 & "> " & update.Title
updatesToInstall.Add(update)
If update.InstallationBehavior.RebootBehavior > 0 Then
rebootMayBeRequired = true
End If
End If
Next
If updatesToInstall.Count = 0 Then
WScript.Echo "No updates were successfully downloaded."
WScript.Quit
End If
If rebootMayBeRequired = true Then
WScript.Echo vbCRLF & "These updates may require a reboot."
End If
WScript.Echo ""
WScript.Echo "Installing updates..."
Set installer = updateSession.CreateUpdateInstaller()
installer.Updates = updatesToInstall
Set installationResult = installer.Install()
'Output results of install
WScript.Echo "Installation Result: " & InstallationResultToText(installationResult.ResultCode)
WScript.Echo "Reboot Required: " & installationResult.RebootRequired & vbCRLF
WScript.Echo "Listing of updates installed and individual installation results:"
For I = 0 to updatesToInstall.Count - 1
WScript.Echo I + 1 & "> " & updatesToInstall.Item(i).Title & ": " & InstallationResultToText(installationResult.GetUpdateResult(i).ResultCode)
Next