SimonS
SimonS

Reputation: 1973

Parameter, if switch is present, don't permit other parameters

I have a function with parameters like this:

param(
    [string[]]$ComputerName,
    [string]$exepath,
    [string[]]$exeargs,
    [switch]$help
)

If the User who uses this function uses the -help switch, there must not be any other parameters declared to call the function. I want to prevent anyone using -help alongside other parameters.

How can I achieve that? I read about_Functions_Advanced_Parameters but it doesn't really help me. I first thought what I need is a ValidateSet but that's different from what I need.

Upvotes: 2

Views: 193

Answers (1)

Martin Brandl
Martin Brandl

Reputation: 59031

You could use the ParameterSetName attribute:

param(
    [Parameter(ParameterSetName='default')]
    [string[]]$ComputerName,

    [Parameter(ParameterSetName='default')]
    [string]$exepath,

    [Parameter(ParameterSetName='default')]
    [string[]]$exeargs,

    [Parameter(ParameterSetName='help')]
    [switch]$help
)

Upvotes: 2

Related Questions