Reputation: 625
I am writing a function in PowerShell 3.0 . One parameter in the function should be set as mandatory depending on the value of another parameter.
The function declaratioin would be as following:
function removeVolumeandArrayFromHost(){
Param( [Parameter(mandatory = $true)] [ValidateSet('iSCSI','FC')] $arrayType, ##IpAddress/Name of the target host
[Parameter(mandatory = $true)] $volumeName, ##Name of the volume to be disconnected. (Volume name as in the array.)
[Parameter(mandatory = $false)] $FCACL, ##Access control list name to which volume is added (Related to FC Array only)
## Work with the parameters given.
}
In the above function, The parameter '$arrayType' can have the value 'iSCSI' or 'FC'.
The parameter $FCACL should be mandatory(mandatory = $true) only if $arrayTpe = 'FC'. if $arrayType = 'iSCSI' then the $FCACL parameter should not be mandatory (mandatory = $false)
I tried using parametersetname but it dosent help as I need to lookinto the value of the parameter $arrayType to decide if $FCACL would be mandatory or not.
Any type of help or pointers would be highly appreciated.
Thanks a lot in advance. :)
Upvotes: 2
Views: 6520
Reputation: 625
The suggestion given by Chris N worked perfectly in my case as I had to deal with only one parameter in this way. My understanding is that Dynamic parameters would be a better option if dealing with many such parameters.
I am adding the code here for much more clarity.
function removeVolumeandArrayFromHost(){
Param( [Parameter(mandatory = $true)] [ValidateSet('iSCSI','FC')] $arrayType, ##IpAddress/Name of the target host
[Parameter(mandatory = $true)] $volumeName, ##Name of the volume to be disconnected. (Volume name as in the array.)
[Parameter(mandatory = $false)] $FCACL) ##Access control list name to which volume is added (Related to FC Array only)
begin{
if (($arrayType -eq 'FC') -and ($FCACL -eq $null)){
Write-Error "For a FC Array, the FCACL parameter cannot be null."
return
}
}
process{
.....
.....
}
}
Thankyou all for your inputs and suggestions. I appreciate your help.
Upvotes: 0
Reputation: 7489
One option is to use the Begin / Process / End portions of the function and handle this kind of stuff in the "Begin" portion, instead of adding a Dynamic Parameter, as other answers have suggested.
Upvotes: 0
Reputation: 13453
You can use Dynamic Parameters, or if you change your $arrayType
variable to a switch, then you can use ParameterSetName
to make things mandatory. So things become like this:
Function removeVolumeandArrayFromHost(){
Param(
[Parameter(ParameterSetName="FC")][switch]$FC,
[Parameter(ParameterSetName="iSCSI")][switch]$iSCSI,
[Parameter(ParameterSetName="FC",Mandatory=$true)] [string]$FCACL,
[Parameter(mandatory = $true)] [string]$volumeName
)
....
## Work with the parameters given.
}
Or, if you know that you are only dealing with either FC or iSCSI, then you only really need one switch statement saying whether or not it is FC or not. If the -FC
switch is present, then it will invoke the ParameterSetName=FC
and require the -FCACL
parameter. If you exclude the -FC
switch, you assume it is iSCSI, and the ParameterSetName
will make sure that you won't require -FCACL
.
Function removeVolumeandArrayFromHost(){
Param(
[Parameter(ParameterSetName="FC")][switch]$FC,
[Parameter(ParameterSetName="FC",Mandatory=$true)] [string]$FCACL,
[Parameter(mandatory = $true)] [string]$volumeName
)
....
## Work with the parameters given.
}
Upvotes: 2