none
Parameter confusion RRS feed

  • Question

  • Hi,

    I'm facing a weird behaviour. When calling this nonsense script ... 

    param (
        [string]$Account,
        [string]$Domain,
        [switch]$SetFlag,
        [pscredential]$Credential
    )
    
    Write-Host 'Account    =' $Account
    Write-Host 'Domain     =' $Domain
    Write-Host 'SetFlag    =' $SetFlag
    Write-Host 'Credential =' $Credential
    

    with these parameters ...

    .\Test-Seltsam.ps1 -Account "ada" -SetFlag $false

    I would never expect this result:

    Account    = ada
    Domain     = False
    SetFlag    = True
    Credential =

    I get this result on my Windows 10 Laptop, but also on a virtual Server 2008 R2 machine.

    Any idea what I'm doing wrong?

    Thanks
    Walter

    Friday, March 22, 2019 5:37 PM

Answers

  • If $SetFag is a switch, you do not pass a value with it. Either include it or not, with no value.

    Richard Mueller - MVP Enterprise Mobility (Identity and Access)

    • Proposed as answer by jrv Friday, March 22, 2019 6:00 PM
    • Marked as answer by WalterFMB Friday, March 22, 2019 6:12 PM
    Friday, March 22, 2019 5:45 PM
    Moderator

All replies

  • If $SetFag is a switch, you do not pass a value with it. Either include it or not, with no value.

    Richard Mueller - MVP Enterprise Mobility (Identity and Access)

    • Proposed as answer by jrv Friday, March 22, 2019 6:00 PM
    • Marked as answer by WalterFMB Friday, March 22, 2019 6:12 PM
    Friday, March 22, 2019 5:45 PM
    Moderator
  • Oh my god, what a shame! I mean: What a SHAME!!!

    Thanks a lot, Richard!

    Friday, March 22, 2019 6:14 PM
  • Very easy to forget. But glad it works as expected now.

    Richard Mueller - MVP Enterprise Mobility (Identity and Access)

    Friday, March 22, 2019 6:19 PM
    Moderator
  • I've mixed up [switch] and [boolean]. That was it!
    When I change the type of $SetFlag, then my script call works fine.

    Friday, March 22, 2019 6:27 PM