Powershell parameters

Kelly Garcia
Kelly Garcia used Ask the Experts™
on
Hi,

I have the this following parameter in my parameters section:

param(
[string]$workspacerg=$null,
[string]$SelectedSubscription=$null,

)

Open in new window


how do i only enable one value on bothe these parameters, i dont want the user to be able to send multiple values
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2007

Commented:
Are you trying to prevent them from using both parameters? If so I think a parameter set is why you need but please confirm.
Shaun VermaakTechnical Specialist
Awarded 2017
Distinguished Expert 2018

Commented:
how do i only enable one value on bothe these parameters, i dont want the user to be able to send multiple values

param(
[string]$workspacerg=$null,
[string]$SelectedSubscription=$null,
)

if ($workspacerg -ne $null -and $SelectedSubscription -ne $null)
{
    # Only specify one error
}

Open in new window

Commented:
I too am not sure exactly what it is that you require.  As you have defined the data type of the Parameters as [String] only one string value will be accepted.  If you require that each parameter has a value in the call you can use attributes to either make the parameter mandatory or validate that it is not null by using either:
[Parameter(Mandatory=$True)]
[ValidateNotNullOrEmpty()]

If the requirement is that only one or the other parameters receives a value then you will, as sirbounty wrote, use the Parameter set attribute so one parameter would have something like:
[Parameter(ParameterSetName='Parameter Set 1')]
and the other:
[Parameter(ParameterSetName='Parameter Set 2')]
That way only one parameter at a time can be used.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial