Function Get-LastUpdates {
Param(
[parameter(Position=0, Mandatory = $false)] [Int32] $DaysAgo = 0,
[parameter(Position=1, Mandatory = $false)] [Int32] $LastNum
)
Process {
$Session = New-Object -ComObject "Microsoft.Update.Session"
$Searcher = $Session.CreateUpdateSearcher()
if ($LastNum) {
$historyCount = $LastNum
} else {
$historyCount = $Searcher.GetTotalHistoryCount()
}
# Limit the returned rows by $historyCount
$HistItems = $Searcher.QueryHistory(0, $historyCount)
#$HistItems | ?{$_.Title -ne $null} |sort Date | Select-Object Date, Title, @{n="desc"; e={$_.Description.substring(0,10)}}, @{name="Operation"; expression={switch($_.operation){ 1 {"Installation"}; 2 {"Uninstallation"}; 3 {"Other"}}}}
#$HistItems |select -First 1
if ($DaysAgo -eq 0) {
$HistItems | ?{$_.Title -ne $null} |sort Date | Select-Object Date, Title
} else {
$HistItems | ?{$_.Title -ne $null} | ?{$_.Date -gt (Get-Date).AddDays(-$DaysAgo) } |sort Date | Select-Object Date, Title
}
# https://itbeco.wordpress.com/microsoft/powershell/
#Select-Object Date, @{expression={$COMPUTERNAME};Label="Host"}, @{name="Operation"; expression={switch($_.operation){1 {"Installation"}; 2 {"Uninstallation"}; 3 {"Other"}}}}, @{name="Status"; expression={switch($_.resultcode){1 {"In Progress"}; 2 {"Succeeded"}; 3 {"Succeeded With Errors"};4 {"Failed"}; 5 {"Aborted"}}}},@{name="Title";expression={[regex]::Match($_.Title,'(KB[0-9]{6,7})').Value}}
}
}
And to run from DOS
@ECHO OFF
if {%1}=={} (
@ECHO USAGE
@ECHO GetLastUpdates DaysAgo
@ECHO.
GOTO :FINISHED
)
Powershell -noprofile -command "&{ . E:\MSSQL\PS\adhoc\Get-LastUpdates.ps1; Get-LastUpdates -DaysAgo %1 }
:FINISHED
