Skip to content

Commit 0ca3ed5

Browse files
committed
Fixes service provider for brackets in service names
1 parent fad86f9 commit 0ca3ed5

File tree

4 files changed

+47
-33
lines changed

4 files changed

+47
-33
lines changed

doc/100-General/10-Changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Released closed milestones can be found on [GitHub](https://github.com/Icinga/ic
1616
* [#729](https://github.com/Icinga/icinga-powershell-framework/issues/729) Fixes `Update-Icinga` to print an error in case a component is not installed, instead of silently continue
1717
* [#734](https://github.com/Icinga/icinga-powershell-framework/issues/734) Fixes a scenario on which a JEA service could become orphaned while manually stopping the Icinga for Windows service, without gracefully shutting down JEA
1818
* [#735](https://github.com/Icinga/icinga-powershell-framework/pull/735) Fixes an issue with filter for EventLog events, which did not properly handle multiple event id includes, causing empty results
19+
* [#745](https://github.com/Icinga/icinga-powershell-framework/pull/745) Fixes an issue for service provider with service names not interpreted correctly in case it contains `[]`
1920

2021
### Enhancements
2122

lib/core/tools/Get-IcingaServices.psm1

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,26 @@ function Get-IcingaServices()
55
[array]$Exclude = @()
66
);
77

8-
$ServiceInformation = Get-Service -Name $Service -ErrorAction SilentlyContinue;
8+
$ServiceInformation = Get-Service;
99
$ServiceWmiInfo = $null;
1010

1111
if ($Service.Count -eq 0) {
1212
$ServiceWmiInfo = Get-IcingaWindowsInformation Win32_Service;
1313
} else {
14-
$ServiceWmiInfo = Get-IcingaWindowsInformation Win32_Service |
14+
try {
15+
$ServiceWmiInfo = Get-IcingaWindowsInformation Win32_Service |
1516
ForEach-Object {
1617
foreach ($svc in $Service) {
1718
if ($_.Name -Like $svc) {
1819
return $_;
1920
}
2021
}
2122
} | Select-Object StartName, Name, ExitCode, StartMode, PathName;
23+
} catch {
24+
Exit-IcingaThrowException -InputString $_.Exception.Message -StringPattern 'wildcard' -ExceptionType 'Input' -ExceptionThrown $IcingaExceptions.Inputs.RegexError;
25+
Exit-IcingaThrowException -CustomMessage $_.Exception.Message -ExceptionType 'Input' -ExceptionThrown $_.Exception.Message;
26+
return $null;
27+
}
2228
}
2329

2430
if ($null -eq $ServiceInformation) {
@@ -27,7 +33,7 @@ function Get-IcingaServices()
2733

2834
[hashtable]$ServiceData = @{ };
2935

30-
foreach ($service in $ServiceInformation) {
36+
foreach ($si in $ServiceInformation) {
3137

3238
[array]$DependentServices = $null;
3339
[array]$DependingServices = $null;
@@ -37,12 +43,12 @@ function Get-IcingaServices()
3743
[int]$StartModeId = 5;
3844
[string]$StartMode = 'Unknown';
3945

40-
if ((Test-IcingaArrayFilter -InputObject $Service.ServiceName -Exclude $Exclude) -eq $FALSE) {
46+
if ((Test-IcingaArrayFilter -InputObject $si.ServiceName -Include $Service -Exclude $Exclude) -eq $FALSE) {
4147
continue;
4248
}
4349

4450
foreach ($wmiService in $ServiceWmiInfo) {
45-
if ($wmiService.Name -eq $service.ServiceName) {
51+
if ($wmiService.Name -eq $si.ServiceName) {
4652
$ServiceUser = $wmiService.StartName;
4753
$ServicePath = $wmiService.PathName;
4854
$ServiceExitCode = $wmiService.ExitCode;
@@ -55,45 +61,45 @@ function Get-IcingaServices()
5561
}
5662

5763
#Dependent / Child
58-
foreach ($dependency in $service.DependentServices) {
64+
foreach ($dependency in $si.DependentServices) {
5965
if ($null -eq $DependentServices) {
6066
$DependentServices = @();
6167
}
6268
$DependentServices += $dependency.Name;
6369
}
6470

6571
#Depends / Parent
66-
foreach ($dependency in $service.ServicesDependedOn) {
72+
foreach ($dependency in $si.ServicesDependedOn) {
6773
if ($null -eq $DependingServices) {
6874
$DependingServices = @();
6975
}
7076
$DependingServices += $dependency.Name;
7177
}
7278

7379
$ServiceData.Add(
74-
$service.Name, @{
80+
$si.Name, @{
7581
'metadata' = @{
76-
'DisplayName' = $service.DisplayName;
77-
'ServiceName' = $service.ServiceName;
78-
'Site' = $service.Site;
79-
'Container' = $service.Container;
80-
'ServiceHandle' = $service.ServiceHandle;
82+
'DisplayName' = $si.DisplayName;
83+
'ServiceName' = $si.ServiceName;
84+
'Site' = $si.Site;
85+
'Container' = $si.Container;
86+
'ServiceHandle' = $si.ServiceHandle;
8187
'Dependent' = $DependentServices;
8288
'Depends' = $DependingServices;
8389
};
8490
'configuration' = @{
85-
'CanPauseAndContinue' = $service.CanPauseAndContinue;
86-
'CanShutdown' = $service.CanShutdown;
87-
'CanStop' = $service.CanStop;
91+
'CanPauseAndContinue' = $si.CanPauseAndContinue;
92+
'CanShutdown' = $si.CanShutdown;
93+
'CanStop' = $si.CanStop;
8894
'Status' = @{
89-
'raw' = [int]$service.Status;
90-
'value' = $service.Status;
95+
'raw' = [int]$si.Status;
96+
'value' = $si.Status;
9197
};
9298
'ServiceType' = @{
93-
'raw' = [int]$service.ServiceType;
94-
'value' = $service.ServiceType;
99+
'raw' = [int]$si.ServiceType;
100+
'value' = $si.ServiceType;
95101
};
96-
'ServiceHandle' = $service.ServiceHandle;
102+
'ServiceHandle' = $si.ServiceHandle;
97103
'StartType' = @{
98104
'raw' = $StartModeId;
99105
'value' = $StartMode;

lib/core/tools/Test-IcingaArrayFilter.psm1

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -88,21 +88,27 @@ function Test-IcingaArrayFilter()
8888

8989
return $FilteredArray;
9090
} else {
91-
foreach ($entry in $Exclude) {
92-
if (([string]$InputObject).ToLower() -Like ([string]$entry).ToLower()) {
93-
return $FALSE;
91+
try {
92+
foreach ($entry in $Exclude) {
93+
if (([string]$InputObject).ToLower() -Like ([string]$entry).ToLower()) {
94+
return $FALSE;
95+
}
9496
}
95-
}
9697

97-
if ($Include.Count -eq 0) {
98-
return $TRUE;
99-
}
98+
if ($Include.Count -eq 0) {
99+
return $TRUE;
100+
}
100101

101-
foreach ($entry in $Include) {
102-
if (([string]$InputObject).ToLower() -Like ([string]$entry).ToLower()) {
103-
$IncludeFound = $TRUE;
104-
break;
102+
foreach ($entry in $Include) {
103+
if (([string]$InputObject).ToLower() -Like ([string]$entry).ToLower()) {
104+
$IncludeFound = $TRUE;
105+
break;
106+
}
105107
}
108+
} catch {
109+
Exit-IcingaThrowException -InputString $_.Exception.Message -StringPattern 'wildcard' -ExceptionType 'Input' -ExceptionThrown $IcingaExceptions.Inputs.RegexError;
110+
Exit-IcingaThrowException -CustomMessage $_.Exception.Message -ExceptionType 'Input' -ExceptionThrown $_.Exception.Message;
111+
return $FALSE;
106112
}
107113

108114
return $IncludeFound;

lib/icinga/exception/Icinga_IcingaExceptionEnums.psm1

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
CimClassNameUnknown = 'The provided class name you try to fetch with Get-CimInstance is not known on this system.';
3232
WmiObjectClassUnknown = 'The provided class name you try to fetch with Get-WmiObject is not known on this system.';
3333
MSSQLCredentialHandling = 'The connection to MSSQL was not possible because your login credential was not correct.';
34-
MSSQLCommandMissing = 'Failed to build a SQL query'
34+
MSSQLCommandMissing = 'Failed to build a SQL query';
35+
RegexError = 'A request was not handled properly because a provided regex could not be interpreted. Please validate your regex and try again. In case you are trying to access a ressource containing [], you will have to escape each symbol by using `. Example: myservice`[`]';
3536
};
3637

3738
[hashtable]$Configuration = @{

0 commit comments

Comments
 (0)