Skip to content

Switch with coloned value parsed differently when invoking with pass thru args #6492

Closed

Description

Setup

a.ps1

&".\b.ps1" @args

b.ps1

Param
(
    [Switch]
    $p1,
    [Switch]
    $p2,
    [String]
    $p3,
    [int]
    $p4
)

echo "p1: $p1"
echo "p2: $p2"
echo "p3: $p3"
echo "p4: $p4"

Execution

👍 Invoke b.ps1 directly:

.\b.ps1 -p1:$true -p2:$true

p1: True
p2: True
p3:
p4: 0

👎 Invoke through a.ps1:

.\a.ps1 -p1:$true -p2:$true

p1: True
p2: True
p3: True
p4: 1

👍 Invoke through a.ps1 removing :$true:

.\b.ps1 -p1 -p2

p1: True
p2: True
p3:
p4: 0

Steve Lee (@SteveL-MSFT), this is our production code and i have reduced and anonymize it to demonstrate the problem. External party (Visual Studio in our case) calls a.ps1 with -p1:$true, so we don't control that part. The only viable solution is to move code from b.ps1 to a.ps1. In our case, that means a lots of duplicate code.

is this a bug with PowerShell argument parsing, splatting or parameter binding? Please share if there is a workaround that can be made in a.ps1 or b.ps2 without raising our expectation from the caller.

Activity

  1. mklement0 commented on Mar 25, 2018

    @mklement0
    Contributor

    kasper3: I think this is a duplicate of #6360 and part of larger discussion about parameter parsing (#6467).

  2. ghost closed this as completedon Mar 25, 2018
  3. mklement0 commented on Mar 25, 2018

    @mklement0
    Contributor

    kasper3:

    The workaround is to define the same param() block in a.ps1 and then pass @PSBoundParameters through instead of @args.

    Of course, if you need to support unknown-in-advance parameters to pass through, that won't work.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Resolution-Duplicate

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions