Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell mandatory parameter depends on another parameter

Tags:

I have a PowerShell function which changes the registry key values. Code:

param(     [Parameter()] [switch]$CreateNewChild,     [Parameter(Mandatory=$true)] [string]$PropertyType ) 

It has a parameter, "CreateNewChild", and if that flag is set, the function will create the key property, even if it wasn't found. The parameter "PropertyType" must be mandatory, but only if "CreateNewChild" flag has been set.

The question is, how do I make a parameter mandatory, but only if another parameter has been specified?

OK, I've been playing around with it. And this does work:

param(   [Parameter(ParameterSetName="one")]   [switch]$DoNotCreateNewChild,    [string]$KeyPath,    [string]$Name,    [string]$NewValue,    [Parameter(ParameterSetName="two")]   [switch]$CreateNewChild,    [Parameter(ParameterSetName="two",Mandatory=$true)]   [string]$PropertyType ) 

However, this means that $KeyPath, $Name and $NewValue are not mandatory any more. Setting "one" parameter set to mandatory breaks the code ("parameter set cannot be resolved" error). These parameter sets are confusing. I'm sure there is a way, but I can't figure out how to do it.

like image 992
simon Avatar asked Nov 23 '12 17:11

simon


People also ask

How do you make a parameter mandatory in PowerShell?

To make a parameter mandatory add a "Mandatory=$true" to the parameter description. To make a parameter optional just leave the "Mandatory" statement out. Make sure the "param" statement is the first one (except for comments and blank lines) in either the script or the function.

How do I pass two parameters in PowerShell?

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 dynamic parameter in PowerShell?

These parameters are added at runtime and are referred to as dynamic parameters because they're only added when needed. For example, you can design a cmdlet that adds several parameters only when a specific switch parameter is specified. Providers and PowerShell functions can also define dynamic parameters.

What is a positional parameter PowerShell?

A positional parameter requires only that you type the arguments in relative order. The system then maps the first unnamed argument to the first positional parameter. The system maps the second unnamed argument to the second unnamed parameter, and so on. By default, all cmdlet parameters are named parameters.


1 Answers

You could group those parameters by defining a parameter set to accomplish this.

param (     [Parameter(ParameterSetName='One')][switch]$CreateNewChild,     [Parameter(ParameterSetName='One',Mandatory=$true)][string]$PropertyType ) 

Reference:

https://devblogs.microsoft.com/powershell/powershell-v2-parametersets

http://blogs.technet.com/b/heyscriptingguy/archive/2011/06/30/use-parameter-sets-to-simplify-powershell-commands.aspx

--- Update ---

Here's a snippet that mimics the functionality you're looking for. The "Extra" parameter set will not be processed unless the -Favorite switch is called.

[CmdletBinding(DefaultParametersetName='None')]  param(      [Parameter(Position=0,Mandatory=$true)] [string]$Age,      [Parameter(Position=1,Mandatory=$true)] [string]$Sex,      [Parameter(Position=2,Mandatory=$true)] [string]$Location,     [Parameter(ParameterSetName='Extra',Mandatory=$false)][switch]$Favorite,           [Parameter(ParameterSetName='Extra',Mandatory=$true)][string]$FavoriteCar )  $ParamSetName = $PsCmdLet.ParameterSetName      Write-Output "Age: $age" Write-Output "Sex: $sex" Write-Output "Location: $Location" Write-Output "Favorite: $Favorite" Write-Output "Favorite Car: $FavoriteCar" Write-Output "ParamSetName: $ParamSetName" 
like image 136
Rex Hardin Avatar answered Oct 03 '22 04:10

Rex Hardin