Last active
July 29, 2024 21:11
-
-
Save ferventcoder/a1e83cb970c8764ec52a1dfdb968f9be to your computer and use it in GitHub Desktop.
Testing PowerShell Fuzzy Parameter matching
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function Get-Input { | |
| [CmdletBinding()] | |
| param ( | |
| [string]$abcdef = 'exe', | |
| [parameter(ValueFromRemainingArguments = $true)][Object[]] $ignoredArguments | |
| ) | |
| $invocaton = $MyInvocation | |
| $argumentsPassed = '' | |
| foreach ($param in $PSBoundParameters.GetEnumerator()) { | |
| $argumentsPassed += "-$($param.Key) '$($param.Value -Join ' ')' " | |
| } | |
| Write-Output "Running $($invocaton.InvocationName) $argumentsPassed $ignoredArguments" | |
| } | |
| Get-Input -abc 'test' | |
| Get-Input -abc 'test' -abcdef 'yes' | |
| Get-Input -abcdef 'yes' -abc 'test' | |
| function Get-InputSimple { | |
| param ( | |
| [string]$abcdef = 'exe' | |
| ) | |
| $invocaton = $MyInvocation | |
| $argumentsPassed = '' | |
| foreach ($param in $PSBoundParameters.GetEnumerator()) { | |
| $argumentsPassed += "-$($param.Key) '$($param.Value -Join ' ')' " | |
| } | |
| Write-Output "Running $($invocaton.InvocationName) $argumentsPassed $args" | |
| } | |
| Get-InputSimple -abc 'test' | |
| Get-InputSimple -abc 'test' -abcdef 'yes' | |
| Get-InputSimple -abcdef 'yes' -abc 'test' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| C:\test> .\get-input.ps1 | |
| Running Get-Input -abcdef 'test' | |
| Get-Input : Cannot bind parameter because parameter 'abcdef' is specified more than once. To provide multiple values to | |
| parameters that can accept multiple values, use the array syntax. For example, "-parameter value1,value2,value3". | |
| At C:\test\get-input.ps1:17 char:23 | |
| + Get-Input -abc 'test' -abcdef 'yes' | |
| + ~~~~~~~ | |
| + CategoryInfo : InvalidArgument: (:) [Get-Input], ParameterBindingException | |
| + FullyQualifiedErrorId : ParameterAlreadyBound,Get-Input | |
| Get-Input : Cannot bind parameter because parameter 'abcdef' is specified more than once. To provide multiple values to | |
| parameters that can accept multiple values, use the array syntax. For example, "-parameter value1,value2,value3". | |
| At C:\test\get-input.ps1:18 char:25 | |
| + Get-Input -abcdef 'yes' -abc 'test' | |
| + ~~~~ | |
| + CategoryInfo : InvalidArgument: (:) [Get-Input], ParameterBindingException | |
| + FullyQualifiedErrorId : ParameterAlreadyBound,Get-Input | |
| Running Get-InputSimple -abcdef 'test' | |
| Get-InputSimple : Cannot bind parameter because parameter 'abcdef' is specified more than once. To provide multiple values to | |
| parameters that can accept multiple values, use the array syntax. For example, "-parameter value1,value2,value3". | |
| At C:\test\get-input.ps1:34 char:29 | |
| + Get-InputSimple -abc 'test' -abcdef 'yes' | |
| + ~~~~~~~ | |
| + CategoryInfo : InvalidArgument: (:) [Get-InputSimple], ParameterBindingException | |
| + FullyQualifiedErrorId : ParameterAlreadyBound,Get-InputSimple | |
| Get-InputSimple : Cannot bind parameter because parameter 'abcdef' is specified more than once. To provide multiple values to | |
| parameters that can accept multiple values, use the array syntax. For example, "-parameter value1,value2,value3". | |
| At C:\test\get-input.ps1:35 char:31 | |
| + Get-InputSimple -abcdef 'yes' -abc 'test' | |
| + ~~~~ | |
| + CategoryInfo : InvalidArgument: (:) [Get-InputSimple], ParameterBindingException | |
| + FullyQualifiedErrorId : ParameterAlreadyBound,Get-InputSimple |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I left out extra parameters being passed as I was not sure it was relevant. Other parameters not specified are either added to
$ignoredArguments(cmdlet binding) or just left to$args.