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!
rsts_supportAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

QlemoBatchelor, Developer and EE Topic AdvisorCommented:
You need two parameter sets, each with one mandatory parameter. I would add both parameters to only one of them.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Member_2_4839798Commented:
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

0
becraigCommented:
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

0
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

footechCommented:
Qlemo's advice is good.  I don't see the others as working.  Here's an example script I threw together.
Param(
    [Parameter(Mandatory = $True, ParameterSetName = "Add")]
    [string]$Add,
    [Parameter(Mandatory = $True, ParameterSetName = "Delete")]
    [Parameter(ParameterSetName = "Add")]
    [string]$Delete
)
Write-Output "Delete: $Delete"
Write-Output "Add: $Add"

Open in new window

0
becraigCommented:
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.
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
becraig., that's not true. This only makes the first parameter mandatory, the second optional.
0
becraigCommented:
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.
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
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.
0
rsts_supportAuthor Commented:
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?
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
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.
0
rsts_supportAuthor Commented:
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).
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
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".
0
rsts_supportAuthor Commented:
Thanks Qlemo, that makes sense. Unfortunately I need to ask another related question. I will do it as a separate question though.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.