I've a Powershell function as follows:
function myfunct {
param(
[Parameter(ParameterSetName="p1")]
[string] $p1,
[Parameter(ParameterSetName="p21")]
[string] $p21,
[Parameter(ParameterSetName="p22")]
[string] $p22
)
# ...
}
This script accepts two configurations of parameters:
By specifying the value of
p1:p1 and [not (p21 and p22)]By specifying the value of
p21andp22:(not p1) and (p21 and p22)
Now, I would like to check their mutual exclusivity. By reading the article PowerShell V2: ParameterSets on MSDN, I found an example about how to properly use the value of $PsCmdlet.ParameterSetName in order to check the specified parameter:
function test-param
{
param(
[Parameter(ParameterSetName="p1",Position=0)]
[DateTime]
$d,
[Parameter(ParameterSetName="p2", Position=0)]
[int]
$i
)
switch ($PsCmdlet.ParameterSetName)
{
"p1" { Write-Host $d; break}
"p2" { Write-Host $i; break}
}
}
According to the the example above, $PsCmdlet.ParameterSetName returns the value of a single parameter, but in my second configuration I need to know if p21 and p22 have been inserted (and if p1 is empty, of course).
Is there a way to perform the parameter validation as desired? If yes, how?
myscript -conf2 p21value p22value?