ParameterSets not being honored

Welcome Forums General PowerShell Q&A ParameterSets not being honored

Viewing 1 reply thread
  • Author
    Posts
    • #206418
      Participant
      Topics: 61
      Replies: 90
      Points: 50
      Rank: Member
      Hi guys,
      I decided to take a stab at trying to do a parameterset.
      param
      (
          [Parameter(Mandatory = $true,
                          Position = 0)]
          [ValidateSet('Production', 'DR')]
          [string]$env,
          [Parameter(Mandatory = $true,
                          Position = 1)]
          [ValidateLength(8, 8)]
          [string]$date,
          [Parameter(Position = 2)]
          [pscredential]$credential,
          [Parameter(ParameterSetName = 'PPgroup')]
          [switch]$PP,
          [switch]$incoming,
          [Parameter(ParameterSetName = 'Agroup')]
          [switch]$A
      )

       

      What i am expecting is that when i use PP that I can only see incoming  . However when I use the parameter a that it does not show incoming. However when I use the parameter a the option for PP goes away but incoming still remains. What gives?
      Any help appreciated.
      • This topic was modified 2 months, 4 weeks ago by WeiYen Tan. Reason: formatting
      • This topic was modified 2 months, 4 weeks ago by Kiran. Reason: Code Formating
      • This topic was modified 2 months, 4 weeks ago by Kiran.
      • This topic was modified 2 months, 4 weeks ago by Kiran.
      • This topic was modified 2 months, 4 weeks ago by Kiran.
    • #206466
      Participant
      Topics: 12
      Replies: 519
      Points: 1,194
      Helping Hand
      Rank: Community Hero

      I would define $date as [Datetime] rather than a [string], and I would separate $pp from $incoming as in:

      function TestParameterSets {
          param
              (
                  [Parameter(Mandatory = $true, Position = 0)]
                  [ValidateSet('Production', 'DR')]
                  [string]$env,
              
                  [Parameter(Mandatory = $true, Position = 1)]
                  [dateTime]$date,
              
                  [Parameter(Position = 2)]
                  [pscredential]$credential,
              
                  [Parameter(ParameterSetName = 'PPgroup')]
                  [switch]$PP,
                  
                  [Parameter(ParameterSetName = 'PPgroup')]
                  [switch]$incoming,
              
                  [Parameter(ParameterSetName = 'Agroup')]
                  [switch]$A
              )
      
          "env        = $env"
          "date       = $date"
          "credential = $(if ($credential) {""(that of '$($credential.UserName)')""} else {$credential}) "
          "pp         = $pp"
          "incoming   = $incoming"
          "A          = $A"
      }
      

      Results match expectations:

      TestParameterSets -env DR -date (Get-Date) -credential (Get-Credential samb) -incoming 
      
      env        = DR
      date       = 02/27/2020 07:17:13
      credential = (that of 'samb')
      pp         = False
      incoming   = True
      A          = False
      
      TestParameterSets -env Production -date (Get-Date) -credential (Get-Credential samb) -A 
      env        = Production
      date       = 02/27/2020 07:13:46
      credential = (that of 'samb')
      pp         = False
      incoming   = False
      A          = True
      

      $Incoming shows as $false when using ‘Agroup’ parameterset because it’s declared as a switch which defaults to $false.

      I would also make at least one of the unique parameters of each parameter set mandatory. For example $PP and $A. To avoid the error: ‘Parameter set cannot be resolved using the specified named parameters.’

      • This reply was modified 2 months, 4 weeks ago by Sam Boutros.
Viewing 1 reply thread
  • You must be logged in to reply to this topic.