Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>My reading of the original question is slightly different to C.B.'s. From</p> <pre><code>Get-Foo [-foo1] &lt;foo1Arg&gt; [-foo2 &lt;foo2Arg&gt; [-bar &lt;barArg&gt;]] </code></pre> <p>The first argument $foo1 is always mandatory, while if $bar is specified $foo2 must be specified too.</p> <p>So my coding of it would be to put $foo1 in both parameter sets.</p> <pre><code>function Get-Foo { [CmdletBinding(DefaultParameterSetName="set1")] param ( [Parameter(ParameterSetName="set1", Mandatory=$true, Position=0)] [Parameter(ParameterSetName="set2", Mandatory=$true, Position=0) ] [string]$foo1, [Parameter(ParameterSetName="set2", Mandatory=$true)] [string]$foo2, [Parameter(ParameterSetName="set2", Mandatory=$false)] [string]$bar ) switch ($PSCmdlet.ParameterSetName) { "set1" { $Output= "Foo is $foo1" } "set2" { if ($bar) { $Output= "Foo is $foo1, Foo2 is $foo2. Bar is $Bar" } else { $Output= "Foo is $foo1, Foo2 is $foo2"} } } Write-Host $Output } Get-Foo -foo1 "Hello" Get-Foo "Hello with no argument switch" Get-Foo "Hello" -foo2 "There is no bar here" Get-Foo "Hello" -foo2 "There" -bar "Three" Write-Host "This Stops for input as foo2 is not specified" Get-Foo -foo1 "Hello" -bar "No foo2" </code></pre> <p>You then get the following output when you run the above.</p> <pre><code>Foo is Hello Foo is Hello with no argument switch Foo is Hello, Foo2 is There is no bar here Foo is Hello, Foo2 is There. Bar is Three This Stops for input as foo2 is not specified cmdlet Get-Foo at command pipeline position 1 Supply values for the following parameters: foo2: Typedfoo2 Foo is Hello, Foo2 is Typedfoo2. Bar is No foo2 </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload