From c7d0a923a24f034134356fe5c181f25a59b6b5ca Mon Sep 17 00:00:00 2001 From: Lord Hepipud Date: Thu, 21 Jan 2021 15:16:43 +0100 Subject: [PATCH] Prefer starttype of services fetching over WMI --- doc/31-Changelog.md | 1 + lib/core/tools/Get-IcingaServices.psm1 | 10 ++++++++-- lib/icinga/enums/Icinga_IcingaEnums.psm1 | 25 ++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/doc/31-Changelog.md b/doc/31-Changelog.md index 5e7e36f..3f6515c 100644 --- a/doc/31-Changelog.md +++ b/doc/31-Changelog.md @@ -18,6 +18,7 @@ Released closed milestones can be found on [GitHub](https://github.com/Icinga/ic * [#186](https://github.com/Icinga/icinga-powershell-framework/issues/186) Fixes path handling for custom local/web path sources for service binary installation * [#188](https://github.com/Icinga/icinga-powershell-framework/pull/188) Removes hardcoded zones `director-global` and `global-zones` which were always set regardless of user specification. This fix will ensure the user has the option to add or not add these zones * [#189](https://github.com/Icinga/icinga-powershell-framework/pull/189) Fixes wrong documented user group for accessing Performance Counter objects which should be `Performance Monitor Users` +* [#192](https://github.com/Icinga/icinga-powershell-framework/pull/192) Fixes code base for `Invoke-IcingaCheckService` by preferring to fetch the startup type of services by using WMI instead of `Get-Services`, as the result of `Get-Services` might be empty in some cases * [#195](https://github.com/Icinga/icinga-powershell-framework/pull/195) Fix Agent installer crash on package lookup with different files in directory * [#197](https://github.com/Icinga/icinga-powershell-framework/pull/197) Fixes progress bar appearance on check outputs for certain plugins, by disabling the entire PowerShell progress bar during the usage of Icinga for Windows diff --git a/lib/core/tools/Get-IcingaServices.psm1 b/lib/core/tools/Get-IcingaServices.psm1 index c5e7dff..266964b 100644 --- a/lib/core/tools/Get-IcingaServices.psm1 +++ b/lib/core/tools/Get-IcingaServices.psm1 @@ -26,6 +26,8 @@ function Get-IcingaServices() [array]$DependingServices = $null; $ServiceExitCode = 0; [string]$ServiceUser = ''; + [int]$StartModeId = 5; + [string]$StartMode = 'Unknown'; if ($Exclude -contains $service.ServiceName) { continue; @@ -35,6 +37,10 @@ function Get-IcingaServices() if ($wmiService.Name -eq $service.ServiceName) { $ServiceUser = $wmiService.StartName; $ServiceExitCode = $wmiService.ExitCode; + if ([string]::IsNullOrEmpty($wmiService.StartMode) -eq $FALSE) { + $StartModeId = ([int]$IcingaEnums.ServiceWmiStartupType[$wmiService.StartMode]); + $StartMode = $IcingaEnums.ServiceStartupTypeName[$StartModeId]; + } break; } } @@ -80,8 +86,8 @@ function Get-IcingaServices() }; 'ServiceHandle' = $service.ServiceHandle; 'StartType' = @{ - 'raw' = [int]$service.StartType; - 'value' = $service.StartType; + 'raw' = $StartModeId; + 'value' = $StartMode; }; 'ServiceUser' = $ServiceUser; 'ExitCode' = $ServiceExitCode; diff --git a/lib/icinga/enums/Icinga_IcingaEnums.psm1 b/lib/icinga/enums/Icinga_IcingaEnums.psm1 index 00951f0..36acd44 100644 --- a/lib/icinga/enums/Icinga_IcingaEnums.psm1 +++ b/lib/icinga/enums/Icinga_IcingaEnums.psm1 @@ -38,6 +38,28 @@ 'c' = 'counter'; }; +<################################################################################################## +################# Service Enums ################################################################## +##################################################################################################> + +[hashtable]$ServiceStartupTypeName = @{ + 0 = 'Boot'; + 1 = 'System'; + 2 = 'Automatic'; + 3 = 'Manual'; + 4 = 'Disabled'; + 5 = 'Unknown'; # Custom +} + +[hashtable]$ServiceWmiStartupType = @{ + 'Boot' = 0; + 'System' = 1; + 'Auto' = 2; + 'Manual' = 3; + 'Disabled' = 4; + 'Unknown' = 5; # Custom +} + <# # Once we defined a new enum hashtable above, simply add it to this list # to make it available within the entire module. @@ -50,6 +72,9 @@ IcingaExitCodeText = $IcingaExitCodeText; IcingaExitCodeColor = $IcingaExitCodeColor; IcingaMeasurementUnits = $IcingaMeasurementUnits; + #services + ServiceStartupTypeName = $ServiceStartupTypeName; + ServiceWmiStartupType = $ServiceWmiStartupType; } Export-ModuleMember -Variable @( 'IcingaEnums' );