Link to home
Start Free TrialLog in
Avatar of rsts_support
rsts_support

asked on

Powershell require at least one of two parameters to be mandatory

Okay this should be super simple but for some reason Googling and searching this site has not provided me with an answer to this question (or more likely I am not providing the correct search terms), so here goes.

Within a Powershell script I want to make sure the user passes a value for at LEAST one parameter out of a possible two when calling a particular Powershell script.

So this should produce an error:

.\scriptname

However these should not:

.\scriptname -add john
.\scriptname -delete fred
.\scriptname -add john -delete fred

I've read fifty different things you can do with parameters (switches, dynamic parameters, parameter sets) but can't seem to figure out how to just make at least one OF two parameters mandatory.  I know how to make single parameter mandatory or to make two parameters mandatory but NOT how to make it so the user has to pass either ONE or TWO parameters but NOT zero).

Any help is much appreciated. Thanks!
ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Member_2_4839798
Member_2_4839798

As an example, this is an extract from one of my scripts:

# Creates A Mandatory Parameters
[CmdletBinding()]
param(
[parameter(mandatory=$true,Position=1)]
[string]$FileExtension,
 
[parameter(mandatory=$false)]
[string]$FileAge,
 
[parameter(mandatory=$true)]
[string]$FilePath
)

Open in new window

Simply declare the parameter to be mandatory

param ([Parameter(Mandatory = $true)]  [string[]]$action1,
	[Parameter(Mandatory = $false)]  [string]$action2)

Open in new window



Above will prompt you for a parameter inline when run, if you want to throw and error then do:
param ([Parameter(Mandatory = $true)]  [string[]]$action1$(throw "Action is mandatory, please provide a value.",
	[Parameter(Mandatory = $false)]  [string]$action2)

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry I seem to have left out "="

param ([Parameter] [string]$action1=$(throw "Action is mandatory, please provide a value."),
	[Parameter]  [string]$action2 )

Open in new window


This will simply provide you the throw you need without circumnavigating the globe.

You can also tweak and play with position of the parameters.

As I indicated in the first post once you set it as Mandatory, the user can simply proceed to add the value in the pipeline.
becraig., that's not true. This only makes the first parameter mandatory, the second optional.
I think I have misread the OP's expectation.

In re-reading it seems he wants to make BOTH params mandatory but either one can be optional once a param is provided ?

In that event I would probably just evaluate for the existence of the value of the parameter before proceeding, from what you are indicating he wants to throw an error if  "-add" is missing and ALSO throw an error if "-delete" is missing but not to error if either -add or -delete is present ?

My approach for this in the past has been to simply use an if statement to evaluate if the param has been populated with a value at runtime.
The recommendation since PS2 is to use the CmdletBInding feature (with parameter attributes). It is more complex, but allows for much better control, e.g. in regard of pipeline data and parameter aliases.  The parameter set declaration allows for automated help output. Also "mandatory" is not hidden somewhere in the code, but visible in the parameter description.
Avatar of rsts_support

ASKER

I tried giving both Qlemo AND footech each credit for the answer but as soon as I gave it to Qlemo it wouldn't allow me to award it to footech also. Thought I pressed "partial" answer for Qlemo but maybe I didn't. Can this be fixed by admin? Thanks.

Also as a comment I would like to say that I don't see where in the documentation for parameter sets in Powershell it says that if you create multiple parameter sets and make them all mandatory that the user only actually has to pass ONE parameter...Did I miss that somewhere in the documentation?
Correct. The documentation doesn't tell you directly and obviously that you can make different parameter combinations mandatory, but that is one of the main purposes.
I assumed that you would click on the "Accept MultipleAnswers" TWICE (one for each answer).  Didn't realize you would be presented with a second screen where you then would have to check the "Expert Comment" for both people you wanted to credit with answering your question.

I guess my problem is that the word "mandatory" in two parameters sounds like BOTH are...you know MANDATORY. In other words, you have to pass arguments for BOTH, not just be permitted to pass EITHER both OR just one (I wouldn't think the latter we be possible).
Since parameter sets are mutually exclusive, you need to consider each as unique parameter definition.

PowerShell will try to find the best fitting parameter set.
 If it finds only an "-Add" parameter when calling, it will accept it as parameter set "Add".
if you provide "-Delete" only, the first parameter set does not work. There is a second one ("Delete"), and that fits.
If you provide both parameters, only the first parameter set contains both parameters. Note that "-Delete" is not mandatory with "Add" parameter set - you need to read it exactly that way: "mandatory for "Delete" parameter set, but not mandatory for "Add".
Thanks Qlemo, that makes sense. Unfortunately I need to ask another related question. I will do it as a separate question though.