icinga-powershell-framework/lib/core/tools/Get-IcingaServices.psm1

116 lines
4.2 KiB
PowerShell
Raw Normal View History

2019-11-03 10:15:46 -05:00
function Get-IcingaServices()
{
param (
[array]$Service,
[array]$Exclude = @()
2019-11-03 10:15:46 -05:00
);
$ServiceInformation = Get-Service;
2019-11-03 10:15:46 -05:00
$ServiceWmiInfo = $null;
if ($Service.Count -eq 0) {
$ServiceWmiInfo = Get-IcingaWindowsInformation Win32_Service;
2019-11-03 10:15:46 -05:00
} else {
try {
$ServiceWmiInfo = Get-IcingaWindowsInformation Win32_Service |
ForEach-Object {
foreach ($svc in $Service) {
if ($_.Name -Like $svc) {
return $_;
}
}
} | Select-Object StartName, Name, ExitCode, StartMode, PathName;
} 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
foreach ($si in $ServiceInformation) {
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 = '';
[int]$StartModeId = 5;
[string]$StartMode = 'Unknown';
2019-11-03 10:15:46 -05:00
if ((Test-IcingaArrayFilter -InputObject $si.ServiceName -Include $Service -Exclude $Exclude) -eq $FALSE) {
continue;
}
2019-11-03 10:15:46 -05:00
foreach ($wmiService in $ServiceWmiInfo) {
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;
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
foreach ($dependency in $si.DependentServices) {
2019-11-03 10:15:46 -05:00
if ($null -eq $DependentServices) {
$DependentServices = @();
}
$DependentServices += $dependency.Name;
}
2019-11-03 10:15:46 -05:00
#Depends / Parent
foreach ($dependency in $si.ServicesDependedOn) {
2019-11-03 10:15:46 -05:00
if ($null -eq $DependingServices) {
$DependingServices = @();
}
$DependingServices += $dependency.Name;
}
$ServiceData.Add(
$si.Name, @{
'metadata' = @{
'DisplayName' = $si.DisplayName;
'ServiceName' = $si.ServiceName;
'Site' = $si.Site;
'Container' = $si.Container;
'ServiceHandle' = $si.ServiceHandle;
'Dependent' = $DependentServices;
'Depends' = $DependingServices;
2019-11-03 10:15:46 -05:00
};
'configuration' = @{
'CanPauseAndContinue' = $si.CanPauseAndContinue;
'CanShutdown' = $si.CanShutdown;
'CanStop' = $si.CanStop;
'Status' = @{
'raw' = [int]$si.Status;
'value' = $si.Status;
2019-11-03 10:15:46 -05:00
};
'ServiceType' = @{
'raw' = [int]$si.ServiceType;
'value' = $si.ServiceType;
2019-11-03 10:15:46 -05:00
};
'ServiceHandle' = $si.ServiceHandle;
'StartType' = @{
'raw' = $StartModeId;
'value' = $StartMode;
2019-11-03 10:15:46 -05:00
};
'ServiceUser' = $ServiceUser;
2021-07-16 15:38:08 -04:00
'ServicePath' = $ServicePath;
'ExitCode' = $ServiceExitCode;
2019-11-03 10:15:46 -05:00
}
}
);
}
return $ServiceData;
}