I wanted to be able to quickly show the Windows Updates on a server. This came about during a discussion about auditing.
Of course, there is no point in re-inventing the wheel so I had a quick Google and found a couple of posts on from Hey Scripting Guy blog and one from Tim Minter. Neither quite did what I wanted so I modified them as follows.
We start by creating a Update object and find the total number of updates and setting them to a variable $History
which we pass to the QueryHistory
Method. This enables us to show all the updates
Passing this to Get-Member
shows
which doesnât show the KB so I read a bit more and found Tom Arbuthnotâs Blog Post
this transforms the ResultCode
Property to something meaningful and places the KB in its own column.
I have created a function called Show-WindowsUpdatesLocal
Itâs Local because doing it for a remote server takes a different approach but I will show that another day.
This means you can call the function and use the results however you like
Show-WindowsUpdatesLocal
Show-WindowsUpdatesLocal| Select Date, HotfixID, Result|Format-Table -AutoSize
Show-WindowsUpdatesLocal|Where-Object {$_.Result -eq âFailedâ} |Select Date, HotfixID, Result,Title|Format-Table -AutoSize
Output to file
Show-WindowsUpdatesLocal|Format-Table -AutoSize|Out-File c:\temp\updates.txt
Output to CSV
Show-WindowsUpdatesLocal|Export-Csv c:\temp\updates.csv
You can get the code here
############################################################# #########
#
# NAME: Show-WindowsUpdatesLocal.ps1
# AUTHOR: Rob Sewell https://blog.robsewell.com
# DATE:22/09/2013
#
# COMMENTS: Load function to show all windows updates locally
#
# USAGE: Show-WindowsUpdatesLocal
# Show-WindowsUpdatesLocal| Select Date, HotfixID, Result|Format-Table -AutoSize
# Show-WindowsUpdatesLocal|Where-Object {$_.Result -eq 'Failed'} |Select Date, HotfixID, Result,Title| Format-Table -AutoSize
# Show-WindowsUpdatesLocal|Format-Table -AutoSize| Out-File c:\temp\updates.txt
# Show-WindowsUpdatesLocal|Export-Csv c:\temp\updates.csv
#
Function Show-WindowsUpdatesLocal {
$Searcher = New-Object -ComObject Microsoft.Update. Searcher
$History = $Searcher.GetTotalHistoryCount()
$Updates = $Searcher.QueryHistory(1, $History)
# Define a new array to gather output
$OutputCollection = @()
Foreach ($update in $Updates) {
$Result = $null
Switch ($update.ResultCode) {
0 { $Result = 'NotStarted'}
1 { $Result = 'InProgress' }
2 { $Result = 'Succeeded' }
3 { $Result = 'SucceededWithErrors' }
4 { $Result = 'Failed' }
5 { $Result = 'Aborted' }
default { $Result = $_ }
}
$string = $update.title
$Regex = âKB\d*â
$KB = $string | Select-String -Pattern $regex | Select-Object { $_.Matches }
$output = New-Object -TypeName PSobject
$output | add-member NoteProperty âDateâ -value $Update.Date
$output | add-member NoteProperty âHotFixIDâ -value $KB.â $_.Matches â.Value
$output | Add-Member NoteProperty "Result" -Value $Result
$output | add-member NoteProperty âTitleâ -value $string
$output | add-member NoteProperty âDescriptionâ -value $update.Description
$OutputCollection += $output
}
$OutputCollection
}