Skip to content

ParameterSet Binding Error in PS5.0+ #2212

@Jaykul

Description

@Jaykul

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.

Metadata

Metadata

Assignees

Labels

In-PRIndicates that a PR is out for the issueKeepOpenThe bot will ignore these and not auto-closeNeeds-InvestigationThe behavior reported in the issue is unexpected and needs further investigation.WG-Enginecore PowerShell engine, interpreter, and runtime

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions