From c199fb594a62ddea9cb84806f8f7f11a76e809ec Mon Sep 17 00:00:00 2001 From: Lord Hepipud Date: Mon, 19 Dec 2022 13:17:36 +0100 Subject: [PATCH] Fixes cmd aliases not evaluated on plugin run --- doc/100-General/10-Changelog.md | 3 ++- .../ConvertTo-IcingaPowerShellArguments.psm1 | 24 +++++++++++++++---- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/doc/100-General/10-Changelog.md b/doc/100-General/10-Changelog.md index 9ae976c..b6cbe66 100644 --- a/doc/100-General/10-Changelog.md +++ b/doc/100-General/10-Changelog.md @@ -7,7 +7,7 @@ documentation before upgrading to a new release. Released closed milestones can be found on [GitHub](https://github.com/Icinga/icinga-powershell-framework/milestones?state=closed). -## 1.10.1 (2022-27-10) +## 1.10.1 (2022-12-20) [Issue and PRs](https://github.com/Icinga/icinga-powershell-framework/milestone/27?closed=1) @@ -15,6 +15,7 @@ Released closed milestones can be found on [GitHub](https://github.com/Icinga/ic * [#582](https://github.com/Icinga/icinga-powershell-framework/issues/582) Fixes background service registration caused by migration task for v1.10.0 being executed even when no services were defined before * [#588](https://github.com/Icinga/icinga-powershell-framework/issues/588) Fixes threshold values causing an error because of too aggressive regex expression +* [#599](https://github.com/Icinga/icinga-powershell-plugins/issues/599) Fixes plugin argument parser to proceed with real argument names and possible provided aliases ## 1.10.0 (2022-08-30) diff --git a/lib/core/tools/ConvertTo-IcingaPowerShellArguments.psm1 b/lib/core/tools/ConvertTo-IcingaPowerShellArguments.psm1 index 44d983a..2ccc067 100644 --- a/lib/core/tools/ConvertTo-IcingaPowerShellArguments.psm1 +++ b/lib/core/tools/ConvertTo-IcingaPowerShellArguments.psm1 @@ -25,13 +25,27 @@ function ConvertTo-IcingaPowerShellArguments() return @{ }; } - $CommandHelp = Get-Help -Name $Command -Full -ErrorAction SilentlyContinue; + $CmdData = Get-Command $Command -ErrorAction SilentlyContinue; + [array]$CmdAllowedArgs = $CmdData.Parameters.Keys; # Ensure we do not cause exceptions along the border in case the plugin is not installed - if ($null -eq $CommandHelp) { + if ($null -eq $CmdAllowedArgs) { return @{ }; } + # Ensure we not only add the parameter name to our allow list but also possible aliases + foreach ($entry in $CmdData.Parameters.Keys) { + if ($CmdData.Parameters[$entry].Aliases.Count -eq 0) { + continue; + } + + foreach ($cmdAlias in $CmdData.Parameters[$entry].Aliases) { + if ($CmdAllowedArgs -NotContains $cmdAlias) { + $CmdAllowedArgs += $cmdAlias; + } + } + } + [hashtable]$IcingaArguments = @{ }; [int]$ArgumentIndex = 0; @@ -50,7 +64,7 @@ function ConvertTo-IcingaPowerShellArguments() } # 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) { + if ($CmdAllowedArgs -Contains ($Arguments[$ArgumentIndex].SubString(1, $Arguments[$ArgumentIndex].Length - 1)) -eq $FALSE) { # Continue if we are not an argument $ArgumentIndex += 1; continue; @@ -77,7 +91,7 @@ function ConvertTo-IcingaPowerShellArguments() # If our next value on the index is an argument in our command # -> The current argument seems to be a switch argument - if ($CommandHelp.parameters.parameter.name -Contains ($NextValue.SubString(1, $NextValue.Length - 1))) { + if ($CmdAllowedArgs -Contains ($NextValue.SubString(1, $NextValue.Length - 1))) { if ($IcingaArguments.ContainsKey($Argument) -eq $FALSE) { $IcingaArguments.Add($Argument, $TRUE); } @@ -103,7 +117,7 @@ function ConvertTo-IcingaPowerShellArguments() [string]$NextValue = $Arguments[$ReadStringIndex + 1]; # 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))) { + if ($CmdAllowedArgs -Contains ($NextValue.SubString(1, $NextValue.Length - 1))) { break; }