Friday, March 11, 2011

Get pending windows updates via Powershell

Gathered some info on how to check for pending windows updates via powershell, below is a generic way to  present pending updates.  I have been trying to use this to make a custom plugin in hyperic.  You could also use this in nagios or any other reporting/monitoring tool or even just email a report to yourself.

#Get All Assigned updates in $SearchResult
$UpdateSession = New-Object -ComObject Microsoft.Update.Session
$UpdateSearcher = $UpdateSession.CreateUpdateSearcher()
$SearchResult = $UpdateSearcher.Search("IsAssigned=1 and IsHidden=0 and IsInstalled=0")


#Matrix Results for type of updates that are needed
$Critical = $SearchResult.updates | where { $_.MsrcSeverity -eq "Critical" }
$important = $SearchResult.updates | where { $_.MsrcSeverity -eq "Important" }
$other = $SearchResult.updates | where { $_.MsrcSeverity -eq $null }

#Write Results
Write-Host "total=$($SearchResult.updates.count)"
Write-Host "critical=$($Critical.count)"
Write-Host "important=$($Important.count)"
Write-Host "other=$($other.count)"


The output of this current script is just to write-host.  you can use the variables in any format that you need.
*After looking at this script I remembered that I got most of this from somewhere else.  I cant seem to find the link that I drew this from but I will reference it if I can find it again.
Post a Comment