Skip to content

Instantly share code, notes, and snippets.

@jacobludriks
Created July 29, 2014 22:48
Show Gist options
  • Star 24 You must be signed in to star a gist
  • Fork 10 You must be signed in to fork a gist
  • Save jacobludriks/9ca9ce61de251a5476f1 to your computer and use it in GitHub Desktop.
Save jacobludriks/9ca9ce61de251a5476f1 to your computer and use it in GitHub Desktop.
wsus-update
Function WSUSUpdate {
$Criteria = "IsInstalled=0 and Type='Software'"
$Searcher = New-Object -ComObject Microsoft.Update.Searcher
try {
$SearchResult = $Searcher.Search($Criteria).Updates
if ($SearchResult.Count -eq 0) {
Write-Output "There are no applicable updates."
exit
}
else {
$Session = New-Object -ComObject Microsoft.Update.Session
$Downloader = $Session.CreateUpdateDownloader()
$Downloader.Updates = $SearchResult
$Downloader.Download()
$Installer = New-Object -ComObject Microsoft.Update.Installer
$Installer.Updates = $SearchResult
$Result = $Installer.Install()
}
}
catch {
Write-Output "There are no applicable updates."
}
}
WSUSUpdate
If ($Result.rebootRequired) { Restart-Computer }
@infosatheesh2020
Copy link

Hi, I am able to run this script in windows server 2008 machine which says that no latest updates available, but when i checked, server shows that
Windows malicious software tool x64 - February 2015 -
was not installed. This update was not detected by this script.

There was other script, http://community.spiceworks.com/scripts/show/1075-download-and-install-updates-with-or-without-reboot which indeed shows the update but getting error while installation (exception calling install with 0 args). I am not very familiar with Powsershell scripting, and so not able to get what the issue is. Can you please help.

@DMikulasr
Copy link

Thank you for this script! I am just starting to learn power shell for my role as a Junior DBA. I ran in to a catch and was hoping you could tell me if my problem is user error or not?

I run the command as listed, however the auto restart does not take place. I am running the script on the machine I am patching, if that matters.

I was also wondering if you found a way to run this against multiple machines at once? Of instance I have to patch 8 DB servers this evening, is it possible to run this on those machines from my laptop (or another server in the domain)?

@PaulWebber
Copy link

Thank you so much for this, you saved me lots of time figuring this all out.

I do have a question though, is there a way to call this on loop after reboots? I'm trying to figure out the easiest way to implement that.
Example:
Run script that gets updates, installs, and restarts if needed. The question is when a server restarts there can sometimes be more updates. Could someone just set this script to run on reboot so it would keep rebooting until there were no more updates?

@jacobludriks
Copy link
Author

sorry, the script is provided as-is. feel free to use it as a building block in your own scripts!

@DiscordDigital
Copy link

Hey that's a cool script. However I can't get it working, all I get is "HResult: 0 and ResultCode: 2", no updates being installed. Any idea why it doesn't seem to work?

@jacobludriks
Copy link
Author

Hey that's a cool script. However I can't get it working, all I get is "HResult: 0 and ResultCode: 2", no updates being installed. Any idea why it doesn't seem to work?

as above, provided aas-is. i am no longer a system administrator and do not deal with wsus anymore.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment