Created
September 26, 2024 08:44
-
-
Save pandieme/ee7f0cf7133f5af77deac923701bf9c5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using namespace System.Collections.Generic | |
param( | |
[string] $ServiceName = 'Spooler' | |
) | |
$ErrorActionPreference = 'Stop' | |
try { | |
[ciminstance[]] $Services = Get-CimInstance -ClassName Win32_Service -Filter "Name = '$ServiceName'" | |
if (-not $Services -or $Services.Count -eq 0) { | |
'No services were found with name {0}' -f $ServiceName | Write-Error | |
} | |
if ($Services.Count -gt 1) { | |
'{0} services were found with name {1}' -f $Services.Count, $ServiceName | Write-Error | |
} | |
[ciminstance] $Service = $Services[0] | |
$ServiceRecovery = & sc.exe qfailure $Service.Name | |
} | |
catch { | |
Write-Warning $_ | |
exit 0 | |
} | |
[Dictionary[[string], [psobject]]] $Items = @{} | |
[string] $LastKey = [string]::Empty | |
foreach ($l in ($ServiceRecovery -split [System.Environment]::NewLine | Select-Object -Skip 1)) { | |
$line = $l.Trim(' ') | |
if ($line.Length -eq 0) { | |
continue | |
} | |
$parts = $line -split ':' | |
if ($parts.Count -eq 2) { | |
[string] $key = $parts[0].Trim(' ') | |
[string] $val = $parts[1].Trim(' ') | |
$script:LastKey = $key | |
if ($key -eq 'FAILURE_ACTIONS') { | |
$Items.Add($key, [List[string]] @($val)) | |
continue | |
} | |
$Items.Add($key, $val) | |
} | |
else { | |
if ($LastKey -eq 'FAILURE_ACTIONS') { | |
$Items.$LastKey.Add($val) | |
continue | |
} | |
$Items.$LastKey = $Items.$LastKey += ', {0}' -f $val | |
} | |
} | |
if (-not [bool] $Items.FAILURE_ACTIONS -or $Items.FAILURE_ACTIONS.Count -eq 0) { | |
'No failure actions detected for service: {0}' -f $Service.Name | Write-Error | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment