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

103 lines
3.7 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 -Name $Service -ErrorAction SilentlyContinue;
$ServiceWmiInfo = $null;
if ($Service.Count -eq 0) {
$ServiceWmiInfo = Get-IcingaWindowsInformation Win32_Service;
2019-11-03 10:15:46 -05:00
} else {
2021-07-16 15:38:08 -04:00
$ServiceWmiInfo = Get-IcingaWindowsInformation Win32_Service | Where-Object { $Service -Contains $_.Name } | Select-Object StartName, Name, ExitCode, StartMode, PathName;
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 ($service 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 ($Exclude -contains $service.ServiceName) {
continue;
}
2019-11-03 10:15:46 -05:00
foreach ($wmiService in $ServiceWmiInfo) {
if ($wmiService.Name -eq $service.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 $service.DependentServices) {
if ($null -eq $DependentServices) {
$DependentServices = @();
}
$DependentServices += $dependency.Name;
}
2019-11-03 10:15:46 -05:00
#Depends / Parent
foreach ($dependency in $service.ServicesDependedOn) {
if ($null -eq $DependingServices) {
$DependingServices = @();
}
$DependingServices += $dependency.Name;
}
$ServiceData.Add(
$service.Name, @{
'metadata' = @{
'DisplayName' = $service.DisplayName;
'ServiceName' = $service.ServiceName;
'Site' = $service.Site;
'Container' = $service.Container;
2019-11-03 10:15:46 -05:00
'ServiceHandle' = $service.ServiceHandle;
'Dependent' = $DependentServices;
'Depends' = $DependingServices;
2019-11-03 10:15:46 -05:00
};
'configuration' = @{
'CanPauseAndContinue' = $service.CanPauseAndContinue;
'CanShutdown' = $service.CanShutdown;
'CanStop' = $service.CanStop;
'Status' = @{
'raw' = [int]$service.Status;
2019-11-03 10:15:46 -05:00
'value' = $service.Status;
};
'ServiceType' = @{
'raw' = [int]$service.ServiceType;
2019-11-03 10:15:46 -05:00
'value' = $service.ServiceType;
};
'ServiceHandle' = $service.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;
}