Multiple parameter sets

Welcome Forums General PowerShell Q&A Multiple parameter sets

Viewing 5 reply threads
  • Author
    Posts
    • #201399
      OK
      Participant
      Topics: 6
      Replies: 17
      Points: 80
      Rank: Member

      How to implement it

      [switch]$a,
      [switch]$b,

      [string]$c,

      [string]$d,
      [string]$e,

      [string]$f

      A C d E
      B C d E
      A C F
      B C F

       

      • This topic was modified 1 month, 3 weeks ago by OK.
      • This topic was modified 1 month, 3 weeks ago by OK.
    • #201414
      Participant
      Topics: 4
      Replies: 70
      Points: 192
      Helping Hand
      Rank: Participant

      Not exactly sure of the question.  Maybe if you provided some example code.  In general though, take a look at the documentation in about_functions_advanced_parameters under the parametersetname argument.  It looks to me like you have 4 parameters and parameter C would not have a parametersetname identified since it is in all three.  For the parameters that appear in more than one parameter set, just include another parameter argument.  Here is an example from the documentation previously referenced:

       

      Param(
      [Parameter(Mandatory=$true,
      ParameterSetName="Computer")]
      [String[]]
      $ComputerName,
      
      [Parameter(Mandatory=$true,
      ParameterSetName="User")]
      [String[]]
      $UserName,
      
      [Parameter(Mandatory=$false, ParameterSetName="Computer")]
      [Parameter(Mandatory=$true, ParameterSetName="User")]
      [Switch]
      $Summary
      )
    • #201420
      OK
      Participant
      Topics: 6
      Replies: 17
      Points: 80
      Rank: Member

      Not exactly sure of the question. Maybe if you provided some example code. In general though, take a look at the documentation in about_functions_advanced_parameters under the parametersetname argument. It looks to me like you have 4 parameters and parameter C would not have a parametersetname identified since it is in all three. For the parameters that appear in more than one parameter set, just include another parameter argument. Here is an example from the documentation previously referenced:

      PowerShell
      16 lines

      <textarea class=”ace_text-input” style=”opacity: 0; height: 18px; width: 7.20125px; left: 52px; top: 0px;” spellcheck=”false” wrap=”off”></textarea>

      <p style=”text-align: left;”>

      I don’t get any inspiration for this example</p>

    • #201429
      Participant
      Topics: 4
      Replies: 70
      Points: 192
      Helping Hand
      Rank: Participant

      Here’s another example.  I think it is exactly what you are asking for.

      function Inspiration {
      Param(
      [Parameter(ParameterSetName="Set1")]
      [Parameter(ParameterSetName="Set3")]
      [switch]
      $A,
      
      [Parameter(ParameterSetName="Set2")]
      [Parameter(ParameterSetName="Set4")]
      [switch]
      $B,
      
      [Parameter(ParameterSetName="Set1")]
      [Parameter(ParameterSetName="Set2")]
      [string]
      $d,
      
      [Parameter(ParameterSetName="Set1")]
      [Parameter(ParameterSetName="Set2")]
      [string]
      $E,
      
      [Parameter(ParameterSetName="Set3")]
      [Parameter(ParameterSetName="Set4")]
      [string]
      $F,
      
      [String]
      $C
      )
      
      Switch ($PSCmdlet.ParameterSetName) {
      "Set1" {"You are in parameter set 1"}
      "Set2" {"You are in parameter set 2"}
      "Set3" {"You are in parameter set 3"}
      "Set4" {"You are in parameter set 4"}
      }
      }
      
      Inspiration -A -C "Hello" -d "World" -E "It's me"
      Inspiration -B -C "Hello" -d "World" -E "It's me"
      Inspiration -A -C "Hello" -f "World"
      Inspiration -B -C "Hello" -F "World"

       

    • #201435
      Senior Moderator
      Topics: 3
      Replies: 121
      Points: 638
      Helping Hand
      Rank: Major Contributor

      You’re going to have to actually explain the problem you’re trying to solve, and show some of your actual code.

    • #201447
      OK
      Participant
      Topics: 6
      Replies: 17
      Points: 80
      Rank: Member

      Good,I can’t understand how it works.

Viewing 5 reply threads
  • You must be logged in to reply to this topic.