PowerShell icon indicating copy to clipboard operation
PowerShell copied to clipboard

ParameterSet Binding Error in PS5.0+

Open Jaykul opened this issue 9 years ago • 5 comments

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.

Jaykul avatar Sep 08 '16 17:09 Jaykul

@daxian-dbw can you please unassign yourself from this unless you're actively working on it?

joeyaiello avatar Nov 02 '16 20:11 joeyaiello

I planed to investigate and work on it, though haven't got to it yet.

daxian-dbw avatar Nov 03 '16 16:11 daxian-dbw

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?

Jaykul avatar May 16 '23 15:05 Jaykul

How can we just ignore this? It still happens in 7.4

Jaykul avatar Nov 17 '23 05:11 Jaykul

Bump

powercode avatar May 18 '24 05:05 powercode