From 32c6f6b2fdb5990837b38d4462b609046ebdee31 Mon Sep 17 00:00:00 2001 From: John G Date: Sat, 14 Oct 2023 16:37:49 +0200 Subject: [PATCH] Update check_windows_updates.ps1 Changes to solve the situation when updates are installed but status is still WARNING with waiting updates --- check_windows_updates.ps1 | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/check_windows_updates.ps1 b/check_windows_updates.ps1 index e2c8ad1..7111b3b 100644 --- a/check_windows_updates.ps1 +++ b/check_windows_updates.ps1 @@ -1,13 +1,13 @@ <# .VERSION -1.3.0 +1.4.0 -.AUTOR +.AUTHOR John Gonzalez #> $script = "check_windows_updates.ps1" -$version = "1.3.0" +$version = "1.4.0" $author = "John Gonzalez" if ($args.Contains("-v")) { @@ -19,18 +19,18 @@ if ($args.Contains("-v")) { $updateSession = New-Object -ComObject Microsoft.Update.Session $updateSearcher = $updateSession.CreateUpdateSearcher() -# Search for all updates -$allUpdates = $updateSearcher.Search("IsHidden=0") +# Search for updates that aren't installed +$notInstalledUpdates = $updateSearcher.Search("IsInstalled=0 and IsHidden=0").Updates # Filter out downloaded and installed updates -$relevantUpdates = $allUpdates.Updates | Where-Object {($_.IsDownloaded -eq $false -or $_.IsInstalled -eq $false) -and $_.Title -notlike "Security Intelligence Update for Microsoft Defender Antivirus*"} +$relevantUpdates = $notInstalledUpdates | Where-Object { $_.Title -notlike "Security Intelligence Update for Microsoft Defender Antivirus*" } # Get the count of available updates $updateCount = $relevantUpdates.Count # Get the count of important and critical updates -$criticalCount = $relevantUpdates | Where-Object {($_.MsrcSeverity -ge "Critical" -and $_.IsHidden -eq $false) -or $_.Title -like "Security Intelligence Update for Microsoft Defender Antivirus*"} | Measure-Object | Select-Object -ExpandProperty Count -$importantCount = $relevantUpdates | Where-Object {$_.MsrcSeverity -eq "Important" -and $_.IsHidden -eq $false -and $_.Title -notlike "Security Intelligence Update for Microsoft Defender Antivirus*"} | Measure-Object | Select-Object -ExpandProperty Count +$criticalCount = $relevantUpdates | Where-Object { ($_.MsrcSeverity -ge "Critical" -and $_.IsHidden -eq $false) -or $_.Title -like "Security Intelligence Update for Microsoft Defender Antivirus*" } | Measure-Object | Select-Object -ExpandProperty Count +$importantCount = $relevantUpdates | Where-Object { $_.MsrcSeverity -eq "Important" -and $_.IsHidden -eq $false -and $_.Title -notlike "Security Intelligence Update for Microsoft Defender Antivirus*" } | Measure-Object | Select-Object -ExpandProperty Count # Set the exit code and message based on the number and severity of updates @@ -43,4 +43,4 @@ if ($updateCount -eq 0) { } else { Write-Host "$updateCount Windows updates are available. $importantCount are important and $criticalCount are critical." exit 2 -} \ No newline at end of file +}