diff --git a/doc/100-General/10-Changelog.md b/doc/100-General/10-Changelog.md index 98ed854..c50d5c3 100644 --- a/doc/100-General/10-Changelog.md +++ b/doc/100-General/10-Changelog.md @@ -22,6 +22,7 @@ Released closed milestones can be found on [GitHub](https://github.com/Icinga/ic * [#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 * [#523](https://github.com/Icinga/icinga-powershell-framework/pull/523) Fixes errors on encapsulated PowerShell calls for missing Cmdlets `Write-IcingaConsoleError` and `Optimize-IcingaForWindowsMemory` * [#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 +* [#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 * [#545](https://github.com/Icinga/icinga-powershell-framework/issues/545) Fixes `RemoteSource` being cleared within repository `.json` files during `Update-IcingaRepository` tasks * [#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 * [#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 diff --git a/lib/core/tools/ConvertTo-IcingaPowerShellArguments.psm1 b/lib/core/tools/ConvertTo-IcingaPowerShellArguments.psm1 index 79ccd2a..4bd93a1 100644 --- a/lib/core/tools/ConvertTo-IcingaPowerShellArguments.psm1 +++ b/lib/core/tools/ConvertTo-IcingaPowerShellArguments.psm1 @@ -11,15 +11,22 @@ The array of arguments for re-encoding. By default, this could be $args for calls from Exit-IcingaExecutePlugin .EXAMPLE - PS> [hashtable]$ConvertedArgs = ConvertTo-IcingaPowerShellArguments -Arguments $args; + PS> [hashtable]$ConvertedArgs = ConvertTo-IcingaPowerShellArguments -Command $CheckCommand -Arguments $args; #> function ConvertTo-IcingaPowerShellArguments() { param ( + [string]$Command = '', [array]$Arguments = @() ); + if ([string]::IsNullOrEmpty($Command)) { + return @{ }; + } + + $CommandHelp = Get-Help -Name $Command -Full; + [hashtable]$IcingaArguments = @{ }; [int]$ArgumentIndex = 0; @@ -37,8 +44,8 @@ function ConvertTo-IcingaPowerShellArguments() continue; } - # Check if it starts with '-', which should indicate it being an argument - if ($Arguments[$ArgumentIndex][0] -ne '-') { + # Check if our string value is a argument contained inside the command being executed + if ($CommandHelp.parameters.parameter.name -Contains ($Arguments[$ArgumentIndex].SubString(1, $Arguments[$ArgumentIndex].Length - 1)) -eq $FALSE) { # Continue if we are not an argument $ArgumentIndex += 1; continue; @@ -52,7 +59,9 @@ function ConvertTo-IcingaPowerShellArguments() # Check if there is anything beyond this argument, if not # -> We are a switch argument, adding TRUE; if (($ArgumentIndex + 1) -ge $Arguments.Count) { - $IcingaArguments.Add($Argument, $TRUE); + if ($IcingaArguments.ContainsKey($Argument) -eq $FALSE) { + $IcingaArguments.Add($Argument, $TRUE); + } $ArgumentIndex += 1; continue; } @@ -61,10 +70,12 @@ function ConvertTo-IcingaPowerShellArguments() if ($Arguments[$ArgumentIndex + 1] -Is [string]) { [string]$NextValue = $Arguments[$ArgumentIndex + 1]; - # If our next value on the index starts with '-', we found another argument + # If our next value on the index is an argument in our command # -> The current argument seems to be a switch argument - if ($NextValue[0] -eq '-') { - $IcingaArguments.Add($Argument, $TRUE); + if ($CommandHelp.parameters.parameter.name -Contains ($NextValue.SubString(1, $NextValue.Length - 1))) { + if ($IcingaArguments.ContainsKey($Argument) -eq $FALSE) { + $IcingaArguments.Add($Argument, $TRUE); + } $ArgumentIndex += 1; continue; } @@ -86,8 +97,8 @@ function ConvertTo-IcingaPowerShellArguments() [string]$NextValue = $Arguments[$ReadStringIndex + 1]; - # In case the next string element starts with '-', this could be an argument - if ($NextValue[0] -eq '-') { + # Check the next string element and evaluate if it is an argument for our command + if ($CommandHelp.parameters.parameter.name -Contains ($NextValue.SubString(1, $NextValue.Length - 1))) { break; } @@ -103,7 +114,9 @@ function ConvertTo-IcingaPowerShellArguments() # Add our argument with the string builder value, in case we had something to add there if ($StringValue.Length -ne 0) { - $IcingaArguments.Add($Argument, (ConvertTo-IcingaUTF8Value -InputObject $StringValue.ToString())); + if ($IcingaArguments.ContainsKey($Argument) -eq $FALSE) { + $IcingaArguments.Add($Argument, (ConvertTo-IcingaUTF8Value -InputObject $StringValue.ToString())); + } $ArgumentIndex += 1; continue; } @@ -114,17 +127,21 @@ function ConvertTo-IcingaPowerShellArguments() # If we are an array object, handle empty arrays if ($Arguments[$ArgumentIndex + 1] -Is [array]) { if ($null -eq $Arguments[$ArgumentIndex + 1] -Or ($Arguments[$ArgumentIndex + 1]).Count -eq 0) { - $IcingaArguments.Add($Argument, @()); + if ($IcingaArguments.ContainsKey($Argument) -eq $FALSE) { + $IcingaArguments.Add($Argument, @()); + } $ArgumentIndex += 1; continue; } } - # Add everything else - $IcingaArguments.Add( - $Argument, - (ConvertTo-IcingaUTF8Value -InputObject $Arguments[$ArgumentIndex + 1]) - ); + if ($IcingaArguments.ContainsKey($Argument) -eq $FALSE) { + # Add everything else + $IcingaArguments.Add( + $Argument, + (ConvertTo-IcingaUTF8Value -InputObject $Arguments[$ArgumentIndex + 1]) + ); + } $ArgumentIndex += 1; } diff --git a/lib/icinga/plugin/Exit-IcingaExecutePlugin.psm1 b/lib/icinga/plugin/Exit-IcingaExecutePlugin.psm1 index edfe7db..08a3962 100644 --- a/lib/icinga/plugin/Exit-IcingaExecutePlugin.psm1 +++ b/lib/icinga/plugin/Exit-IcingaExecutePlugin.psm1 @@ -5,7 +5,7 @@ function Exit-IcingaExecutePlugin() ); # We need to fix the argument encoding hell - [hashtable]$ConvertedArgs = ConvertTo-IcingaPowerShellArguments -Arguments $args; + [hashtable]$ConvertedArgs = ConvertTo-IcingaPowerShellArguments -Command $Command -Arguments $args; [string]$JEAProfile = Get-IcingaJEAContext; [bool]$CheckByIcingaForWindows = $FALSE; [bool]$CheckByJEAShell = $FALSE;