locked
Parameter sets not working in Powershell 4 that worked in version 2 RRS feed

  • Question

  • In version 2, the following worked:

    [CmdletBinding(DefaultParameterSetName="Credential")]
    Param(
       [Parameter(Position=0,Mandatory=$true,ParameterSetName="Credential")]
       [System.Management.Automation.PSCredential]$Credential

       [Parameter(Position=0,Mandatory=$true,ParameterSetName="Identity")]
       [Security.Principal.WindowsIdentity]$Identity

       [Parameter(Position=0,Mandatory=$true,ParameterSetName="Password")]
       [string]$Name
    ,
       [Parameter(Position=1,Mandatory=$true,ParameterSetName="Password")]
       [Alias("PW")]
       $Password = (Read-Host "Password" -AsSecureString)
    ,
       [Parameter(Position=2,Mandatory=$false,ParameterSetName="Password")]
       [string]$Domain
    ,
       [Alias("PT")] 
       [switch]$Passthru
    )

    If I ran the function with this parameter set and passed a Credential object to it, it would function as expected with the Credential parameter set.

    In version 4, the Read-Host "Password" -AsSecureString segment runs. Since I passed a Credential, it should have recognized the Credential parameter set and not processed the Password parameter set parameters.

    Am I right that this is a bug going from PS 2 to PS 4 (or maybe even 3)? Or am I missing something?

    Thursday, May 21, 2015 11:21 PM

Answers

  • You cannot have both mandatory and a default at the same time:

        [Alias("PW")]
        [Parameter(
            Position = 1,
            Mandatory = $true,
            ParameterSetName = "Password"
        )]$Password = (Read-Host "Password" -AsSecureString),

    This should work:

    [CmdletBinding(DefaultParameterSetName = 'Credential')]
    Param (
    
        [Parameter(
            Position = 0, 
            Mandatory = $true, 
            ParameterSetName = 'Credential'
        )][System.Management.Automation.PSCredential]$Credential,
    
        [Parameter(
            Position = 0, 
            Mandatory = $true, 
            ParameterSetName = 'Identity'
        )][Security.Principal.WindowsIdentity]$Identity,
        
        [Parameter(
            Position = 0,
            Mandatory = $true,
            ParameterSetName = 'Password'
        )][string]$Name,
        
        [Alias('PW')]
        [Parameter(
            Position = 1,
            Mandatory = $true, 
            ParameterSetName = 'Password'
        )]$Password,
    
        [Parameter(
            Position = 2,
            ParameterSetName = 'Password'
        )][string]$Domain,
    
        [Alias('PT')]
        [switch]$Passthru
    )
    
    


    \_(ツ)_/

    • Marked as answer by Bill_Stewart Tuesday, July 21, 2015 6:06 PM
    Friday, May 22, 2015 12:07 AM

All replies

  • You cannot have both mandatory and a default at the same time:

        [Alias("PW")]
        [Parameter(
            Position = 1,
            Mandatory = $true,
            ParameterSetName = "Password"
        )]$Password = (Read-Host "Password" -AsSecureString),

    This should work:

    [CmdletBinding(DefaultParameterSetName = 'Credential')]
    Param (
    
        [Parameter(
            Position = 0, 
            Mandatory = $true, 
            ParameterSetName = 'Credential'
        )][System.Management.Automation.PSCredential]$Credential,
    
        [Parameter(
            Position = 0, 
            Mandatory = $true, 
            ParameterSetName = 'Identity'
        )][Security.Principal.WindowsIdentity]$Identity,
        
        [Parameter(
            Position = 0,
            Mandatory = $true,
            ParameterSetName = 'Password'
        )][string]$Name,
        
        [Alias('PW')]
        [Parameter(
            Position = 1,
            Mandatory = $true, 
            ParameterSetName = 'Password'
        )]$Password,
    
        [Parameter(
            Position = 2,
            ParameterSetName = 'Password'
        )][string]$Domain,
    
        [Alias('PT')]
        [switch]$Passthru
    )
    
    


    \_(ツ)_/

    • Marked as answer by Bill_Stewart Tuesday, July 21, 2015 6:06 PM
    Friday, May 22, 2015 12:07 AM
  • Ok, you're right. Thanks. That probably never should have been that way. I had to make it not mandatory and then put in the read-host further down in the script. So, the script worked in an earlier version, but was never quite right in the first place.
    Friday, May 22, 2015 2:58 AM
  • That was an old hack from V1. With V2 and later we need to use different methods as you have seen.  With  each version the rules can get tighter in some areas.  Whenever I see something like that with older code I immediate try to simplify the code and look for possible syntax and usage violations.

    \_(ツ)_/


    • Edited by jrv Friday, May 22, 2015 3:04 AM
    Friday, May 22, 2015 3:04 AM