I have been working on getting the windows update template days since last update installed, to work consistently for a while now.
The script did not account for update history missing, and the datediff cmd was getting error for server updated today.
I added logic for missing history and gave it a statistic of 1242, with message of Update History Does not Exist, then I updated datedif to use new-TimeSpan fixing the statistic for servers updated in the last 24 hours.
The issue I have now is windows 2016 servers are set to install Definition update for windows defender automatically, I need to exclude 'definition update' from the $objEntry list because the fact the definition update was installed is not releavent to if the server was actually patched.
any ideas on how to filter out 'Definition Update for Windows Defender' in the objsearcher?
try {
Get-Service wuauserv |Set-Service -StartupType Automatic |Start-Service
$objSession = New-Object -com "Microsoft.Update.Session"
$objSearcher= $objSession.CreateUpdateSearcher()
$colHistory = $objSearcher.QueryHistory(0, 1)
}
catch
{
Write-Host "ERROR: $($Error[0])";
exit 1;
}
Foreach($objEntry in $colHistory)
{
$patch = $objEntry.Title
$patchdate = $objEntry.Date.tostring("yyyy.MM.dd")
}
if ($patchdate -eq $null)
{
Write-Host "Message: Update history does not exist."
Write-Host "Statistic:" 1242
exit 0
}
$today = get-date -uformat "%Y.%m.%d"
try {
$patchdiff = New-TimeSpan -End $today -Start $patchdate
}
catch
{
Write-Host "ERROR: $($Error[0])";
exit 1;
}
Write-Host "Message: Last installed update:" $patch
Write-Host "Statistic:" $patchdiff.Days
exit 0