Fixes negative thresholds interpreted as argument

This commit is contained in:
Lord Hepipud 2022-08-22 17:15:25 +02:00
parent 11deff3403
commit 8c5f9b5f21
3 changed files with 35 additions and 17 deletions

View file

@ -19,6 +19,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
### Enhancements

View file

@ -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;
}

View file

@ -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;