Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mutually exclusive powershell parameters

SCENARIO

  • I'm writing a cmdlet for Powershell 2.0 using Visual Studio 2008 and .NET 3.5
  • the cmdlet requires 3 arguments.

my intended grammar of the cmdlet is something like this:

cmdletname [foo|bar] p1, p2 
  • That reads as the user must give a value for "-foo" or "-bar" but can't give both together.

EXAMPLE OF VALID INPUT

cmdletname -foo xxx -p1 hello  -p2 world cmdletname -bar yyy -p1 hello  -p2 world 

EXAMPLE OF INVALID INPUT

cmdletname -foo xxx -bar yyy -p1 hello  -p2 world 

MY QUESTION

  • My question is on how to do this in powershell so that it does all the checking for me - or if that is possible at all.
  • I know I can use just have two optional parameters for foo and bar and simply do the error checking manually. That's how I have it implemented currently.
  • Alternatively, I am interested in suggestions for different approaches.
like image 359
namenlos Avatar asked Nov 19 '09 23:11

namenlos


People also ask

How do I set parameters in PowerShell?

To create a parameter set, you must specify the ParameterSetName keyword of the Parameter attribute for every parameter in the parameter set. For parameters that belong to multiple parameter sets, add a Parameter attribute for each parameter set.

How do I pass multiple parameters to a PowerShell script?

To pass multiple parameters you must use the command line syntax that includes the names of the parameters. For example, here is a sample PowerShell script that runs the Get-Service function with two parameters. The parameters are the name of the service(s) and the name of the Computer.

What is param () in PowerShell?

The PowerShell parameter is a fundamental component of any script. A parameter is a way that developers enable script users to provide input at runtime. If a PowerShell script's behavior needs to change in some way, a parameter provides an opportunity to do so without changing the underlying code.


1 Answers

You can use the parameter attribute to declare multiple parameter sets. You then simply assign parameters that are mutually exclusive to different parameter sets.

EDIT:

This is also documented in 'about_Functions_Advanced_Parameters', under the section "ParameterSetName Named Argument". This is how different sets of parameters is handled with cmdlets like Get-Random (which has mutually exclusive parameters):

> get-random -input 4 -max 77 Get-Random : Parameter set cannot be resolved using the specified named parameters. At line:1 char:11 + get-random <<<<  -input 4 -max 77     + CategoryInfo          : InvalidArgument: (:) [Get-Random], ParameterBindingException     + FullyQualifiedErrorId : AmbiguousParameterSet,Microsoft.PowerShell.Commands.GetRandomCommand 

Here's an example of doing it in a function:

function exclusive_params() {      param(          [parameter(ParameterSetName="seta")]$one,         [parameter(ParameterSetName="setb")]$two,          $three      )     "one: $one"; "two: $two"; "three: $three"  } 

The parameters one and two are in different parameter sets, so they cannot be specified together:

> exclusive_params -one foo -two bar -three third exclusive_params : Parameter set cannot be resolved using the specified named parameters. At line:1 char:17 + exclusive_params <<<<  -one foo -two bar -three third     + CategoryInfo          : InvalidArgument: (:) [exclusive_params], ParameterBindingException     + FullyQualifiedErrorId : AmbiguousParameterSet,exclusive_params 

Which is the same error I got with Get-Random. But I can use the parameters independently:

> exclusive_params -one foo -three third one: foo two: three: third 

...or:

> exclusive_params -two bar -three third one: two: bar three: third 
like image 121
Joey Avatar answered Sep 21 '22 10:09

Joey