Skip to content

Commit 0e9874f

Browse files
committed
Prefer starttype of services fetching over WMI
1 parent 876791d commit 0e9874f

File tree

3 files changed

+49
-19
lines changed

3 files changed

+49
-19
lines changed

doc/31-Changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Released closed milestones can be found on [GitHub](https://github.com/Icinga/ic
2323

2424
* [#123](https://github.com/Icinga/icinga-powershell-plugins/pull/123) Fixes wrong documented user group for accessing Performance Counter objects which should be `Performance Monitor Users`
2525
* [#124](https://github.com/Icinga/icinga-powershell-plugins/pull/124) Fixes crash on `Invoke-IcingaCheckService` if an automatic service is not running
26+
* [#126](https://github.com/Icinga/icinga-powershell-plugins/pull/126) 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
2627

2728
## 1.3.0 (2020-12-01)
2829

provider/enums/Icinga_ProviderEnums.psm1

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -863,9 +863,30 @@
863863
}
864864

865865
[hashtable]$ServiceStartupType = @{
866+
'Boot' = 0;
867+
'System' = 1;
866868
'Automatic' = 2;
867869
'Manual' = 3;
868870
'Disabled' = 4;
871+
'Unknown' = 5; # Custom
872+
}
873+
874+
[hashtable]$ServiceStartupTypeName = @{
875+
0 = 'Boot';
876+
1 = 'System';
877+
2 = 'Automatic';
878+
3 = 'Manual';
879+
4 = 'Disabled';
880+
5 = 'Unknown'; # Custom
881+
}
882+
883+
[hashtable]$ServiceWmiStartupType = @{
884+
'Boot' = 0;
885+
'System' = 1;
886+
'Auto' = 2;
887+
'Manual' = 3;
888+
'Disabled' = 4;
889+
'Unknown' = 5; # Custom
869890
}
870891

871892
<##################################################################################################
@@ -1013,6 +1034,8 @@
10131034
ServiceStatus = $ServiceStatus;
10141035
ServiceStatusName = $ServiceStatusName;
10151036
ServiceStartupType = $ServiceStartupType;
1037+
ServiceStartupTypeName = $ServiceStartupTypeName;
1038+
ServiceWmiStartupType = $ServiceWmiStartupType;
10161039
#/lib/provider/tasks
10171040
ScheduledTaskStatus = $ScheduledTaskStatus;
10181041
ScheduledTaskName = $ScheduledTaskName;

provider/services/Icinga_ProviderServices.psm1

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ function Get-IcingaServices()
2626
[array]$DependingServices = $null;
2727
$ServiceExitCode = 0;
2828
[string]$ServiceUser = '';
29+
[int]$StartModeId = 5;
30+
[string]$StartMode = 'Unknown';
2931

3032
if ($Exclude -contains $service.ServiceName) {
3133
continue;
@@ -35,6 +37,10 @@ function Get-IcingaServices()
3537
if ($wmiService.Name -eq $service.ServiceName) {
3638
$ServiceUser = $wmiService.StartName;
3739
$ServiceExitCode = $wmiService.ExitCode;
40+
if ([string]::IsNullOrEmpty($wmiService.StartMode) -eq $FALSE) {
41+
$StartModeId = ([int]$ProviderEnums.ServiceWmiStartupType[$wmiService.StartMode]);
42+
$StartMode = $ProviderEnums.ServiceStartupTypeName[$StartModeId];
43+
}
3844
break;
3945
}
4046
}
@@ -57,34 +63,34 @@ function Get-IcingaServices()
5763

5864
$ServiceData.Add(
5965
$service.Name, @{
60-
'metadata' = @{
61-
'DisplayName' = $service.DisplayName;
62-
'ServiceName' = $service.ServiceName;
63-
'Site' = $service.Site;
64-
'Container' = $service.Container;
66+
'metadata' = @{
67+
'DisplayName' = $service.DisplayName;
68+
'ServiceName' = $service.ServiceName;
69+
'Site' = $service.Site;
70+
'Container' = $service.Container;
6571
'ServiceHandle' = $service.ServiceHandle;
66-
'Dependent' = $DependentServices;
67-
'Depends' = $DependingServices;
72+
'Dependent' = $DependentServices;
73+
'Depends' = $DependingServices;
6874
};
6975
'configuration' = @{
7076
'CanPauseAndContinue' = $service.CanPauseAndContinue;
71-
'CanShutdown' = $service.CanShutdown;
72-
'CanStop' = $service.CanStop;
73-
'Status' = @{
74-
'raw' = [int]$service.Status;
77+
'CanShutdown' = $service.CanShutdown;
78+
'CanStop' = $service.CanStop;
79+
'Status' = @{
80+
'raw' = [int]$service.Status;
7581
'value' = $service.Status;
7682
};
77-
'ServiceType' = @{
78-
'raw' = [int]$service.ServiceType;
83+
'ServiceType' = @{
84+
'raw' = [int]$service.ServiceType;
7985
'value' = $service.ServiceType;
8086
};
81-
'ServiceHandle' = $service.ServiceHandle;
82-
'StartType' = @{
83-
'raw' = [int]$service.StartType;
84-
'value' = $service.StartType;
87+
'ServiceHandle' = $service.ServiceHandle;
88+
'StartType' = @{
89+
'raw' = $StartModeId;
90+
'value' = $StartMode;
8591
};
86-
'ServiceUser' = $ServiceUser;
87-
'ExitCode' = $ServiceExitCode;
92+
'ServiceUser' = $ServiceUser;
93+
'ExitCode' = $ServiceExitCode;
8894
}
8995
}
9096
);

0 commit comments

Comments
 (0)