Skip to content

Instantly share code, notes, and snippets.

@ferventcoder
Last active May 14, 2022 02:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ferventcoder/a1e83cb970c8764ec52a1dfdb968f9be to your computer and use it in GitHub Desktop.
Save ferventcoder/a1e83cb970c8764ec52a1dfdb968f9be to your computer and use it in GitHub Desktop.
Testing PowerShell Fuzzy Parameter matching
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'
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
@ferventcoder
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment