2019-11-03 10:15:46 -05:00
|
|
|
function Get-IcingaServices()
|
|
|
|
|
{
|
|
|
|
|
param (
|
2020-02-18 06:27:47 -05:00
|
|
|
[array]$Service,
|
|
|
|
|
[array]$Exclude = @()
|
2019-11-03 10:15:46 -05:00
|
|
|
);
|
|
|
|
|
|
2024-08-15 09:05:43 -04:00
|
|
|
$ServiceInformation = Get-Service;
|
2019-11-03 10:15:46 -05:00
|
|
|
$ServiceWmiInfo = $null;
|
|
|
|
|
|
|
|
|
|
if ($Service.Count -eq 0) {
|
2020-07-27 05:45:02 -04:00
|
|
|
$ServiceWmiInfo = Get-IcingaWindowsInformation Win32_Service;
|
2019-11-03 10:15:46 -05:00
|
|
|
} else {
|
2024-08-15 09:05:43 -04:00
|
|
|
try {
|
|
|
|
|
$ServiceWmiInfo = Get-IcingaWindowsInformation Win32_Service |
|
2022-01-26 12:18:24 -05:00
|
|
|
ForEach-Object {
|
|
|
|
|
foreach ($svc in $Service) {
|
|
|
|
|
if ($_.Name -Like $svc) {
|
|
|
|
|
return $_;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} | Select-Object StartName, Name, ExitCode, StartMode, PathName;
|
2024-08-15 09:05:43 -04:00
|
|
|
} catch {
|
|
|
|
|
Exit-IcingaThrowException -InputString $_.Exception.Message -StringPattern 'wildcard' -ExceptionType 'Input' -ExceptionThrown $IcingaExceptions.Inputs.RegexError;
|
|
|
|
|
Exit-IcingaThrowException -CustomMessage $_.Exception.Message -ExceptionType 'Input' -ExceptionThrown $_.Exception.Message;
|
|
|
|
|
return $null;
|
|
|
|
|
}
|
2019-11-03 10:15:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($null -eq $ServiceInformation) {
|
|
|
|
|
return $null;
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-04 08:59:28 -04:00
|
|
|
[hashtable]$ServiceData = @{ };
|
2019-11-03 10:15:46 -05:00
|
|
|
|
2024-08-15 09:05:43 -04:00
|
|
|
foreach ($si in $ServiceInformation) {
|
2020-08-04 08:48:32 -04:00
|
|
|
|
2019-11-03 10:15:46 -05:00
|
|
|
[array]$DependentServices = $null;
|
|
|
|
|
[array]$DependingServices = $null;
|
2020-02-18 07:36:46 -05:00
|
|
|
$ServiceExitCode = 0;
|
2019-11-03 10:15:46 -05:00
|
|
|
[string]$ServiceUser = '';
|
2021-07-16 15:38:08 -04:00
|
|
|
[string]$ServicePath = '';
|
2021-01-21 09:16:43 -05:00
|
|
|
[int]$StartModeId = 5;
|
|
|
|
|
[string]$StartMode = 'Unknown';
|
2019-11-03 10:15:46 -05:00
|
|
|
|
2024-08-15 09:05:43 -04:00
|
|
|
if ((Test-IcingaArrayFilter -InputObject $si.ServiceName -Include $Service -Exclude $Exclude) -eq $FALSE) {
|
2020-02-18 06:27:47 -05:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-03 10:15:46 -05:00
|
|
|
foreach ($wmiService in $ServiceWmiInfo) {
|
2024-08-15 09:05:43 -04:00
|
|
|
if ($wmiService.Name -eq $si.ServiceName) {
|
2020-02-18 07:36:46 -05:00
|
|
|
$ServiceUser = $wmiService.StartName;
|
2021-07-16 15:38:08 -04:00
|
|
|
$ServicePath = $wmiService.PathName;
|
2020-02-18 07:36:46 -05:00
|
|
|
$ServiceExitCode = $wmiService.ExitCode;
|
2021-01-21 09:16:43 -05:00
|
|
|
if ([string]::IsNullOrEmpty($wmiService.StartMode) -eq $FALSE) {
|
|
|
|
|
$StartModeId = ([int]$IcingaEnums.ServiceWmiStartupType[$wmiService.StartMode]);
|
|
|
|
|
$StartMode = $IcingaEnums.ServiceStartupTypeName[$StartModeId];
|
|
|
|
|
}
|
2019-11-03 10:15:46 -05:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#Dependent / Child
|
2024-08-15 09:05:43 -04:00
|
|
|
foreach ($dependency in $si.DependentServices) {
|
2019-11-03 10:15:46 -05:00
|
|
|
if ($null -eq $DependentServices) {
|
|
|
|
|
$DependentServices = @();
|
|
|
|
|
}
|
|
|
|
|
$DependentServices += $dependency.Name;
|
|
|
|
|
}
|
2020-08-04 08:48:32 -04:00
|
|
|
|
2019-11-03 10:15:46 -05:00
|
|
|
#Depends / Parent
|
2024-08-15 09:05:43 -04:00
|
|
|
foreach ($dependency in $si.ServicesDependedOn) {
|
2019-11-03 10:15:46 -05:00
|
|
|
if ($null -eq $DependingServices) {
|
|
|
|
|
$DependingServices = @();
|
|
|
|
|
}
|
|
|
|
|
$DependingServices += $dependency.Name;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$ServiceData.Add(
|
2024-08-15 09:05:43 -04:00
|
|
|
$si.Name, @{
|
2020-08-04 09:13:04 -04:00
|
|
|
'metadata' = @{
|
2024-08-15 09:05:43 -04:00
|
|
|
'DisplayName' = $si.DisplayName;
|
|
|
|
|
'ServiceName' = $si.ServiceName;
|
|
|
|
|
'Site' = $si.Site;
|
|
|
|
|
'Container' = $si.Container;
|
|
|
|
|
'ServiceHandle' = $si.ServiceHandle;
|
2020-08-04 09:13:04 -04:00
|
|
|
'Dependent' = $DependentServices;
|
|
|
|
|
'Depends' = $DependingServices;
|
2019-11-03 10:15:46 -05:00
|
|
|
};
|
|
|
|
|
'configuration' = @{
|
2024-08-15 09:05:43 -04:00
|
|
|
'CanPauseAndContinue' = $si.CanPauseAndContinue;
|
|
|
|
|
'CanShutdown' = $si.CanShutdown;
|
|
|
|
|
'CanStop' = $si.CanStop;
|
2020-08-04 09:13:04 -04:00
|
|
|
'Status' = @{
|
2024-08-15 09:05:43 -04:00
|
|
|
'raw' = [int]$si.Status;
|
|
|
|
|
'value' = $si.Status;
|
2019-11-03 10:15:46 -05:00
|
|
|
};
|
2020-08-04 09:13:04 -04:00
|
|
|
'ServiceType' = @{
|
2024-08-15 09:05:43 -04:00
|
|
|
'raw' = [int]$si.ServiceType;
|
|
|
|
|
'value' = $si.ServiceType;
|
2019-11-03 10:15:46 -05:00
|
|
|
};
|
2024-08-15 09:05:43 -04:00
|
|
|
'ServiceHandle' = $si.ServiceHandle;
|
2020-08-04 09:13:04 -04:00
|
|
|
'StartType' = @{
|
2021-01-21 09:16:43 -05:00
|
|
|
'raw' = $StartModeId;
|
|
|
|
|
'value' = $StartMode;
|
2019-11-03 10:15:46 -05:00
|
|
|
};
|
2020-08-04 09:13:04 -04:00
|
|
|
'ServiceUser' = $ServiceUser;
|
2021-07-16 15:38:08 -04:00
|
|
|
'ServicePath' = $ServicePath;
|
2020-08-04 09:13:04 -04:00
|
|
|
'ExitCode' = $ServiceExitCode;
|
2019-11-03 10:15:46 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return $ServiceData;
|
|
|
|
|
}
|