-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Description
When binding with multiple parameter sets, the binder confuses position and sets the wrong value on the parameter.
This bug exists since at least PS 4
Steps to reproduce
function Test-Parameter {
[CmdletBinding()]
param
(
[Parameter(ParameterSetName = 'Two', Position = 0)]
[Parameter(ParameterSetName = 'One', Position = 1)]
[string]
$First,
[Parameter(ParameterSetName = 'Two', Position = 1)]
[string]
$Second
)
"ParameterSet " + $PSCmdlet.ParameterSetName
$PSBoundParameters
}
Test-Parameter Hello World
Expected behavior
The expected output is:
ParameterSet Two
Key Value
--- -----
First Hello
Second World
Actual behavior
The $First parameter is incorrectly bound to the same value as the $Second parameter:
ParameterSet Two
Key Value
--- -----
First World
Second World
Environment data
This happens on PowerShell 4, 5, and 6.0.0-alpha.9
Experimental Notes
The bug is caused by having the Position index of the first parameter in the non-matched parameter set be the same as the Position index of the second parameter in the matched parameter set.
That is, if you change [Parameter(ParameterSetName = 'One', Position = 1)] to [Parameter(ParameterSetName = 'One', Position = 0)] it is does not occur
And if you change [Parameter(ParameterSetName = 'Two', Position = 1)] to [Parameter(ParameterSetName = 'Two', Position = 10)] it does not occur
But if you change them both to, say, Position = 5 then it does occur. Even if there are other parameters and other parameter sets.
And of course, if you specify the parameter name when you call it, or if the parameters are of different types, then this does not occur.