Created
March 4, 2016 00:48
-
-
Save mbeall/46542e61a3fac30be4d4 to your computer and use it in GitHub Desktop.
Windows Update Fix
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Set updateSession = CreateObject("Microsoft.Update.Session") | |
updateSession.ClientApplicationID = "MSDN Sample Script" | |
Set updateSearcher = updateSession.CreateUpdateSearcher() | |
WScript.Echo "Searching for updates..." & vbCRLF | |
Set searchResult = _ | |
updateSearcher.Search("IsInstalled=0 and Type='Software' and IsHidden=0") | |
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 + 1 & "> " & update.Title | |
Next | |
If searchResult.Updates.Count = 0 Then | |
WScript.Echo "There are no applicable updates." | |
WScript.Quit | |
End If | |
WScript.Echo vbCRLF & "Creating collection of updates to download:" | |
Set updatesToDownload = CreateObject("Microsoft.Update.UpdateColl") | |
For I = 0 to searchResult.Updates.Count-1 | |
Set update = searchResult.Updates.Item(I) | |
addThisUpdate = false | |
If update.InstallationBehavior.CanRequestUserInput = true Then | |
WScript.Echo I + 1 & "> skipping: " & update.Title & _ | |
" because it requires user input" | |
Else | |
If update.EulaAccepted = false Then | |
WScript.Echo I + 1 & "> note: " & update.Title & _ | |
" has a license agreement that must be accepted:" | |
WScript.Echo update.EulaText | |
WScript.Echo "Do you accept this license agreement? (Y/N)" | |
strInput = WScript.StdIn.Readline | |
WScript.Echo | |
If (strInput = "Y" or strInput = "y") Then | |
update.AcceptEula() | |
addThisUpdate = true | |
Else | |
WScript.Echo I + 1 & "> skipping: " & update.Title & _ | |
" because the license agreement was declined" | |
End If | |
Else | |
addThisUpdate = true | |
End If | |
End If | |
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 searchResult.Updates.Count-1 | |
set update = searchResult.Updates.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 vbCRLF & "Would you like to install updates now? (Y/N)" | |
strInput = WScript.StdIn.Readline | |
WScript.Echo | |
If (strInput = "Y" or strInput = "y") Then | |
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 | |
Next | |
End If |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Set updateSession = CreateObject("Microsoft.Update.Session") | |
updateSession.ClientApplicationID = "MSDN Sample Script" | |
'Get update title to search for | |
WScript.Echo "Enter the title of the update: " & _ | |
"(for example, Update for Windows Rights Management client 1.0)" | |
updateTitle = WScript.StdIn.Readline | |
WScript.Echo vbCRLF & "Searching for: " & updateTitle & "..." | |
Set updateSearcher = updateSession.CreateupdateSearcher() | |
'Search for all software updates, already installed and not installed | |
Set searchResult = _ | |
updateSearcher.Search("Type='Software'") | |
Set updateToInstall = CreateObject("Microsoft.Update.UpdateColl") | |
updateIsApplicable = False | |
'Cycle through search results to look for the update title | |
For i = 0 To searchResult.Updates.Count-1 | |
Set update = searchResult.Updates.Item(i) | |
If UCase(update.Title) = UCase(updateTitle) Then | |
'Update in list of applicable updates | |
'Determine if it is already installed or not | |
If update.IsInstalled = False Then | |
WScript.Echo vbCRLF & _ | |
"Result: Update applicable, not installed." | |
updateIsApplicable = True | |
updateToInstall.Add(update) | |
Else | |
'Update is installed so notify user and quit | |
WScript.Echo vbCRLF & _ | |
"Result: Update applicable, already installed." | |
updateIsApplicable = True | |
WScript.Quit | |
End If | |
End If | |
Next | |
If updateIsApplicable = False Then | |
WScript.Echo "Result: Update is not applicable to this machine." | |
WScript.Quit | |
End If | |
WScript.Echo vbCRLF & "Would you like to install now? (Y/N)" | |
stdInput = WScript.StdIn.Readline | |
If (strInput = "N" or strInput = "n") Then | |
WScript.Quit | |
ElseIf (stdInput = "Y" OR stdInput = "y") Then | |
'Download update | |
Set downloader = updateSession.CreateUpdateDownloader() | |
downloader.Updates = updateToInstall | |
WScript.Echo vbCRLF & "Downloading..." | |
Set downloadResult = downloader.Download() | |
WScript.Echo "Download Result: " & downloadResult.ResultCode | |
'Install Update | |
Set installer = updateSession.CreateUpdateInstaller() | |
WScript.Echo vbCRLF & "Installing..." | |
installer.Updates = updateToInstall | |
Set installationResult = installer.Install() | |
'Output the result of the installation | |
WScript.Echo "Installation Result: " & _ | |
installationResult.ResultCode | |
WScript.Echo "Reboot Required: " & _ | |
installationResult.RebootRequired | |
End If |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment