Knows Not Much
Knows Not Much

Reputation: 31526

can a parameter belong to multiple parameter sets?

I have this code

Param(

  [Parameter(ParameterSetName='InsertException')]
  [switch]$insert,

  [Parameter(ParameterSetName='UpdateException')]
  [switch]$update,

  [Parameter(ParameterSetName='GetException')]
  [switch]$get,

  [Parameter(Mandatory=$True, ParameterSetName='UpdateException')]
  [string]$userName
 )

I want that parameter $userName is mandatory when the switch -update or -insert is used but parameter $username should not be asked when switch -get is used.

How can I do this?

Upvotes: 1

Views: 101

Answers (1)

Keith Hill
Keith Hill

Reputation: 201592

Look at the help topic about_functions_advanced_parameters and it will show you how to do that:

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
)

Upvotes: 3

Related Questions