ParameterSet Binding Error in PS5.0+
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.
@daxian-dbw can you please unassign yourself from this unless you're actively working on it?
I planed to investigate and work on it, though haven't got to it yet.
I recognize that this probably feels like something you meant to do, at this point, but can't we please fix this, or at least make it error or warn that things have gone badly wrong?
How can we just ignore this? It still happens in 7.4
Bump