From 2f5b9208e81c593e375e932efe62f419aea3bf13 Mon Sep 17 00:00:00 2001 From: Nick Dimou Date: Fri, 27 Nov 2026 06:26:59 +0200 Subject: [PATCH] Apply often-stopped service exceptions to all exit codes --- health-tests/HealthTest-Services.ps1 | 10 +- tests/Unit/HealthTest-Services.Tests.ps1 | 114 +++++++++++++++++++++++ 2 files changed, 119 insertions(+), 5 deletions(-) create mode 100644 tests/Unit/HealthTest-Services.Tests.ps1 diff --git a/health-tests/HealthTest-Services.ps1 b/health-tests/HealthTest-Services.ps1 index 7142ceb..a1d69c6 100644 --- a/health-tests/HealthTest-Services.ps1 +++ b/health-tests/HealthTest-Services.ps1 @@ -74,16 +74,16 @@ Uses: Win32_Service, Get-ServiceVendors. if ($not_started_services) { $not_started_services | %{ - # TODO: consider exitcode 1077 practicly equivalent to 0 (no problem) - # 1077 = No attempts to start the service have been made since the last boot. $exitCodeMeaning = Get-ServiceExitCodeMessage $_.ExitCode $serviceInListOfOftenStoped = ( ($_.name -in $SERVICES_THAT_ARE_OFTEN_STOPPED) -or ($_.name -match $SERVICES_THAT_ARE_OFTEN_STOPPED_REGEX) ) - if ($serviceInListOfOftenStoped -and ($_.ExitCode -in (0,1077))) { - $hadIssue = $true - Write-Warning "[info] This service is stoped but its last execution terminated NORMALY and it's one of the services that are often stopped: Service '$($_.Name)', StartMode=$($_.StartMode), DelayedAutoStart=$($_.DelayedAutoStart), last ExitCode=$($_.ExitCode)($exitCodeMeaning)."} else { + # Often-stopped services remain informational regardless of their last exit code. + if ($serviceInListOfOftenStoped) { + $hadIssue = $true + Write-Warning "[info] This service is stopped and is one of the services that are often stopped: Service '$($_.Name)', StartMode=$($_.StartMode), DelayedAutoStart=$($_.DelayedAutoStart), last ExitCode=$($_.ExitCode)($exitCodeMeaning)." + } else { if ($_.ExitCode -in (0,1077)) { # Use NOTICE here, even though it is noisier than INFO, because a service # that stays stopped after restart should remain visible instead of being suppressed. diff --git a/tests/Unit/HealthTest-Services.Tests.ps1 b/tests/Unit/HealthTest-Services.Tests.ps1 new file mode 100644 index 0000000..737bf19 --- /dev/null +++ b/tests/Unit/HealthTest-Services.Tests.ps1 @@ -0,0 +1,114 @@ +Describe 'HealthTest-Services stopped-service exceptions' { + BeforeAll { + $repoRoot = Split-Path -Parent (Split-Path -Parent $PSScriptRoot) + . (Join-Path $repoRoot 'health-tests\HealthTest-Services.ps1') + } + + BeforeEach { + $script:stoppedServices = @() + Mock Get-CimInstance { $script:stoppedServices } -ParameterFilter { + $ClassName -eq 'Win32_Service' -and $Filter -eq "StartMode='Auto' and State!='Running'" + } + Mock Get-ServiceVendors { @() } + } + + It 'reports as INFO with exit code ' -ForEach @( + foreach ($serviceName in @( + 'edgeupdate', 'InventorySvc', 'MapsBroker', 'sppsvc', 'gupdate', + 'dmwappushservice', 'RemoteRegistry', 'StateRepository', 'gpsvc', + 'AppXSvc', 'TrustedInstaller', 'mapsbroker', + 'GoogleUpdaterInternalService123.0.1.2', 'GoogleUpdaterService123.0.1.2' + )) { + foreach ($serviceExitCode in @(0, 1077, 50, 1067, 987654)) { + @{ ServiceName = $serviceName; ExitCode = $serviceExitCode } + } + } + ) { + $script:stoppedServices = @([pscustomobject]@{ + Name = $ServiceName + DisplayName = 'Often stopped service' + State = 'Stopped' + StartMode = 'Auto' + DelayedAutoStart = $true + ExitCode = $ExitCode + }) + + $findings = @(HealthTest-Services 3>&1) + + $findings | Should -HaveCount 1 + $findings[0].Message | Should -Match '^\[info\]' + $findings[0].Message | Should -Match ([regex]::Escape("Service '$ServiceName'")) + $findings[0].Message | Should -Match 'StartMode=Auto, DelayedAutoStart=True' + $findings[0].Message | Should -Match ([regex]::Escape("ExitCode=$ExitCode(")) + $findings[0].Message | Should -Not -Match 'terminated normally|terminated normaly' + Should -Invoke Get-CimInstance -Times 1 -Exactly -ParameterFilter { + $ClassName -eq 'Win32_Service' -and $Filter -eq "StartMode='Auto' and State!='Running'" + } + Should -Invoke Get-ServiceVendors -Times 1 -Exactly + } + + It 'keeps for unlisted service with exit code ' -ForEach @( + @{ ServiceName = 'MSSQLSERVER'; ExitCode = 0; Level = 'NOTICE' } + @{ ServiceName = 'MSSQLSERVER'; ExitCode = 1077; Level = 'NOTICE' } + @{ ServiceName = 'MSSQLSERVER'; ExitCode = 50; Level = 'FAILURE' } + @{ ServiceName = 'MSSQLSERVER'; ExitCode = 1067; Level = 'FAILURE' } + @{ ServiceName = 'MSSQLSERVER'; ExitCode = 987654; Level = 'FAILURE' } + @{ ServiceName = 'MapsBrokerExtra'; ExitCode = 50; Level = 'FAILURE' } + @{ ServiceName = 'GoogleUpdaterService'; ExitCode = 50; Level = 'FAILURE' } + @{ ServiceName = 'GoogleUpdaterService123.0Extra'; ExitCode = 50; Level = 'FAILURE' } + @{ ServiceName = 'ExtraGoogleUpdaterInternalService123.0'; ExitCode = 50; Level = 'FAILURE' } + ) { + $script:stoppedServices = @([pscustomobject]@{ + Name = $ServiceName + DisplayName = 'Unlisted service' + State = 'Stopped' + StartMode = 'Auto' + DelayedAutoStart = $false + ExitCode = $ExitCode + }) + + $findings = @(HealthTest-Services 3>&1) + + $findings | Should -HaveCount 1 + $findings[0].Message | Should -Match "^\[$Level\]" + $findings[0].Message | Should -Match ([regex]::Escape("Service '$ServiceName'")) + $findings[0].Message | Should -Match ([regex]::Escape("ExitCode=$ExitCode(")) + } + + It 'still reports a broken payload for an excepted service' { + $script:stoppedServices = @([pscustomobject]@{ + Name = 'MapsBroker' + DisplayName = 'Downloaded Maps Manager' + State = 'Stopped' + StartMode = 'Auto' + DelayedAutoStart = $true + ExitCode = 50 + }) + Mock Get-ServiceVendors { + [pscustomobject]@{ ServiceName = 'MapsBroker'; ExceptionsThrown = 'Missing payload' } + } + + $findings = @(HealthTest-Services 3>&1) + + $findings | Should -HaveCount 2 + $findings[0].Message | Should -Match '^\[info\]' + $findings[1].Message | Should -Match "^\[FAILURE\] Service 'MapsBroker' has a broken or unresolved executable/payload path\." + $findings[1].Message | Should -Match 'Missing payload' + } + + It 'still reports a payload inspection error' { + Mock Get-ServiceVendors { throw 'Inspection failed' } + + $findings = @(HealthTest-Services 3>&1) + + $findings | Should -HaveCount 1 + $findings[0].Message | Should -Be '[FAILURE] Could not inspect service executable/payload paths: Inspection failed' + } + + It 'reports PASS when there are no stopped auto-start services or broken payloads' { + $findings = @(HealthTest-Services 3>&1) + + $findings | Should -HaveCount 1 + $findings[0].Message | Should -Be '[PASS] Services healthy: all auto-start services are running and service payload paths resolved.' + } +}