Skip to content

Commit 4c5eeaf

Browse files
authored
Merge pull request #550 from Icinga:fix/negative_thresholds_interpreted_as_argument
Fix: Negative thresholds interpreted as argument Fixes negative thresholds being interpreted wrongly as argument instead of an value for an argument
2 parents 786fa9f + 8c5f9b5 commit 4c5eeaf

File tree

3 files changed

+35
-17
lines changed

3 files changed

+35
-17
lines changed

doc/100-General/10-Changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Released closed milestones can be found on [GitHub](https://github.com/Icinga/ic
2222
* [#529](https://github.com/Icinga/icinga-powershell-framework/pull/529) Fixes package manifest reader for Icinga for Windows components on Windows 2012 R2 and older
2323
* [#523](https://github.com/Icinga/icinga-powershell-framework/pull/523) Fixes errors on encapsulated PowerShell calls for missing Cmdlets `Write-IcingaConsoleError` and `Optimize-IcingaForWindowsMemory`
2424
* [#524](https://github.com/Icinga/icinga-powershell-framework/issues/524) Fixes uninstallation process by improving the location handling of PowerShell instances with Icinga IMC or Shell
25+
* [#528](https://github.com/Icinga/icinga-powershell-framework/issues/528) Fixes negative thresholds being interpreted wrongly as argument instead of an value for an argument
2526
* [#545](https://github.com/Icinga/icinga-powershell-framework/issues/545) Fixes `RemoteSource` being cleared within repository `.json` files during `Update-IcingaRepository` tasks
2627
* [#552](https://github.com/Icinga/icinga-powershell-framework/pull/552) Fixes file encoding for Icinga for Windows v1.10.0 to ensure the cache is always properly created with the correct encoding
2728
* [#553](https://github.com/Icinga/icinga-powershell-framework/pull/553) Fixes an exception caused by service recovery setting, if the required service was not installed before

lib/core/tools/ConvertTo-IcingaPowerShellArguments.psm1

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,22 @@
1111
The array of arguments for re-encoding. By default, this could be $args
1212
for calls from Exit-IcingaExecutePlugin
1313
.EXAMPLE
14-
PS> [hashtable]$ConvertedArgs = ConvertTo-IcingaPowerShellArguments -Arguments $args;
14+
PS> [hashtable]$ConvertedArgs = ConvertTo-IcingaPowerShellArguments -Command $CheckCommand -Arguments $args;
1515
#>
1616

1717
function ConvertTo-IcingaPowerShellArguments()
1818
{
1919
param (
20+
[string]$Command = '',
2021
[array]$Arguments = @()
2122
);
2223

24+
if ([string]::IsNullOrEmpty($Command)) {
25+
return @{ };
26+
}
27+
28+
$CommandHelp = Get-Help -Name $Command -Full;
29+
2330
[hashtable]$IcingaArguments = @{ };
2431
[int]$ArgumentIndex = 0;
2532

@@ -37,8 +44,8 @@ function ConvertTo-IcingaPowerShellArguments()
3744
continue;
3845
}
3946

40-
# Check if it starts with '-', which should indicate it being an argument
41-
if ($Arguments[$ArgumentIndex][0] -ne '-') {
47+
# Check if our string value is a argument contained inside the command being executed
48+
if ($CommandHelp.parameters.parameter.name -Contains ($Arguments[$ArgumentIndex].SubString(1, $Arguments[$ArgumentIndex].Length - 1)) -eq $FALSE) {
4249
# Continue if we are not an argument
4350
$ArgumentIndex += 1;
4451
continue;
@@ -52,7 +59,9 @@ function ConvertTo-IcingaPowerShellArguments()
5259
# Check if there is anything beyond this argument, if not
5360
# -> We are a switch argument, adding TRUE;
5461
if (($ArgumentIndex + 1) -ge $Arguments.Count) {
55-
$IcingaArguments.Add($Argument, $TRUE);
62+
if ($IcingaArguments.ContainsKey($Argument) -eq $FALSE) {
63+
$IcingaArguments.Add($Argument, $TRUE);
64+
}
5665
$ArgumentIndex += 1;
5766
continue;
5867
}
@@ -61,10 +70,12 @@ function ConvertTo-IcingaPowerShellArguments()
6170
if ($Arguments[$ArgumentIndex + 1] -Is [string]) {
6271
[string]$NextValue = $Arguments[$ArgumentIndex + 1];
6372

64-
# If our next value on the index starts with '-', we found another argument
73+
# If our next value on the index is an argument in our command
6574
# -> The current argument seems to be a switch argument
66-
if ($NextValue[0] -eq '-') {
67-
$IcingaArguments.Add($Argument, $TRUE);
75+
if ($CommandHelp.parameters.parameter.name -Contains ($NextValue.SubString(1, $NextValue.Length - 1))) {
76+
if ($IcingaArguments.ContainsKey($Argument) -eq $FALSE) {
77+
$IcingaArguments.Add($Argument, $TRUE);
78+
}
6879
$ArgumentIndex += 1;
6980
continue;
7081
}
@@ -86,8 +97,8 @@ function ConvertTo-IcingaPowerShellArguments()
8697

8798
[string]$NextValue = $Arguments[$ReadStringIndex + 1];
8899

89-
# In case the next string element starts with '-', this could be an argument
90-
if ($NextValue[0] -eq '-') {
100+
# Check the next string element and evaluate if it is an argument for our command
101+
if ($CommandHelp.parameters.parameter.name -Contains ($NextValue.SubString(1, $NextValue.Length - 1))) {
91102
break;
92103
}
93104

@@ -103,7 +114,9 @@ function ConvertTo-IcingaPowerShellArguments()
103114

104115
# Add our argument with the string builder value, in case we had something to add there
105116
if ($StringValue.Length -ne 0) {
106-
$IcingaArguments.Add($Argument, (ConvertTo-IcingaUTF8Value -InputObject $StringValue.ToString()));
117+
if ($IcingaArguments.ContainsKey($Argument) -eq $FALSE) {
118+
$IcingaArguments.Add($Argument, (ConvertTo-IcingaUTF8Value -InputObject $StringValue.ToString()));
119+
}
107120
$ArgumentIndex += 1;
108121
continue;
109122
}
@@ -114,17 +127,21 @@ function ConvertTo-IcingaPowerShellArguments()
114127
# If we are an array object, handle empty arrays
115128
if ($Arguments[$ArgumentIndex + 1] -Is [array]) {
116129
if ($null -eq $Arguments[$ArgumentIndex + 1] -Or ($Arguments[$ArgumentIndex + 1]).Count -eq 0) {
117-
$IcingaArguments.Add($Argument, @());
130+
if ($IcingaArguments.ContainsKey($Argument) -eq $FALSE) {
131+
$IcingaArguments.Add($Argument, @());
132+
}
118133
$ArgumentIndex += 1;
119134
continue;
120135
}
121136
}
122137

123-
# Add everything else
124-
$IcingaArguments.Add(
125-
$Argument,
126-
(ConvertTo-IcingaUTF8Value -InputObject $Arguments[$ArgumentIndex + 1])
127-
);
138+
if ($IcingaArguments.ContainsKey($Argument) -eq $FALSE) {
139+
# Add everything else
140+
$IcingaArguments.Add(
141+
$Argument,
142+
(ConvertTo-IcingaUTF8Value -InputObject $Arguments[$ArgumentIndex + 1])
143+
);
144+
}
128145

129146
$ArgumentIndex += 1;
130147
}

lib/icinga/plugin/Exit-IcingaExecutePlugin.psm1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ function Exit-IcingaExecutePlugin()
55
);
66

77
# We need to fix the argument encoding hell
8-
[hashtable]$ConvertedArgs = ConvertTo-IcingaPowerShellArguments -Arguments $args;
8+
[hashtable]$ConvertedArgs = ConvertTo-IcingaPowerShellArguments -Command $Command -Arguments $args;
99
[string]$JEAProfile = Get-IcingaJEAContext;
1010
[bool]$CheckByIcingaForWindows = $FALSE;
1111
[bool]$CheckByJEAShell = $FALSE;

0 commit comments

Comments
 (0)