Skip to content

Instantly share code, notes, and snippets.

@mrknmc
Forked from jacobludriks/wsus-update.ps1
Last active August 29, 2016 04:21
Show Gist options
  • Save mrknmc/bea8cb9fe2d9aa1397049dbb332daa00 to your computer and use it in GitHub Desktop.
Save mrknmc/bea8cb9fe2d9aa1397049dbb332daa00 to your computer and use it in GitHub Desktop.
wsus-update
Function WSUSUpdate {
$LogFile = "UpdateLogs\" + (Get-Date -format yyyy-MM-dd--HH-mm) + ".log"
Start-transcript $LogFile
$Criteria = "IsInstalled=0 and Type='Software'"
$Searcher = New-Object -ComObject Microsoft.Update.Searcher
try {
$SearchResult = $Searcher.Search($Criteria)
[Object[]] $Critical = $SearchResult.updates | where { $_.MsrcSeverity -eq "Critical" }
[Object[]] $Important = $SearchResult.updates | where { $_.MsrcSeverity -eq "Important" }
[Object[]] $Moderate = $SearchResult.updates | where { $_.MsrcSeverity -eq "Moderate" }
[Object[]] $Low = $SearchResult.updates | where { $_.MsrcSeverity -eq "Low" }
[Object[]] $Unspecified = $SearchResult.updates | where { $_.MsrcSeverity -eq "Unspecified" }
[Object[]] $Other = $SearchResult.updates | where { $_.MsrcSeverity -eq $null }
#Write Results
"Critical : $($Critical.count)"
"Important : $($Important.count)"
"Moderate : $($Moderate.count)"
"Low : $($Low.count)"
"Unspecified : $($Unspecified.count)"
"Other : $($Other.count)"
"Total : $($SearchResult.updates.count)"
""
"Notes: ""BrowseOnly"" updates are considered optional."
" Microsoft anti-virus updates include sub-updates"
" that are already installed, so size is inaccurate."
# "If" statement in Expression: http://blogs.technet.com/b/josebda/archive/2014/04/19/powershell-tips-for-building-objects-with-custom-properties-and-special-formatting.aspx
# Formatting number as MB: https://technet.microsoft.com/en-us/library/ff730948.aspx
$UpdatesToInstall = $Critical + $Important + $Moderate + $Low
if ($UpdatesToInstall.Count -eq 0) {
Write-Output "There are no applicable updates."
exit
}
else {
$Session = New-Object -ComObject Microsoft.Update.Session
$Downloader = $Session.CreateUpdateDownloader()
$Downloader.Updates = $UpdatesToInstall
$Downloader.Download()
$Installer = New-Object -ComObject Microsoft.Update.Installer
$Installer.Updates = $UpdatesToInstall
$Result = $Installer.Install()
}
}
catch {
Write-Output "There are no applicable updates."
}
finally {
Stop-transcript
}
}
WSUSUpdate
If ($Result.rebootRequired) { Restart-Computer }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment