I don't think you can do this with parameter sets and mandatory parameters. If you defined 3 different parameter sets with alternating mandatory parameter like this:
Param(
[Parameter(Mandatory=$true)]
[string]$Text,
[Parameter(Mandatory=$true, ParameterSetName='o1')]
[Parameter(Mandatory=$false, ParameterSetName='o2')]
[Parameter(Mandatory=$false, ParameterSetName='o3')]
[Switch][bool]$Switch1,
[Parameter(Mandatory=$false, ParameterSetName='o1')]
[Parameter(Mandatory=$true, ParameterSetName='o2')]
[Parameter(Mandatory=$false, ParameterSetName='o3')]
[Switch][bool]$Switch2,
[Parameter(Mandatory=$false, ParameterSetName='o1')]
[Parameter(Mandatory=$false, ParameterSetName='o2')]
[Parameter(Mandatory=$true, ParameterSetName='o3')]
[Switch][bool]$Switch3
)
it works if you use just one of the switches:
My-Function "foo" -Switch2
but fails if you use more than one switch:
PS C:\> .\test.ps1 "foo" -Switch1 -Switch2
C:\test.ps1 : Parameter set cannot be resolved using the specified named
parameters.
At line:1 char:1
+ .\test.ps1 "foo" -Switch1 -Switch2
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [test.ps1], ParameterBindingException
+ FullyQualifiedErrorId : AmbiguousParameterSet,test.ps1
I'd use a set-validated mandatory parameter instead:
Param(
[Parameter(Mandatory=$true)]
[string]$Text,
[Parameter(Mandatory=$true)]
[ValidateSet('Switch1', 'Switch2', 'Switch3', ignorecase=$true)]
[string[]]$Options
)
That would allow you to call the function like this:
My-Function "foo" -Options Switch1
My-Function "foo" -Options Switch2,Switch3
Or you could make all three switches optional and validate them inside the function:
Param(
[Parameter(Mandatory=$true)]
[string]$Text,
[Parameter(Mandatory=$false)]
[Switch][bool]$Switch1,
[Parameter(Mandatory=$false)]
[Switch][bool]$Switch2,
[Parameter(Mandatory=$false)]
[Switch][bool]$Switch3
)
if (-not ($Switch1.IsPresent -or $Switch2.IsPresent -or $Switch3.IsPresent)) {
throw 'Missing switch'
}
Dynamic parameters might be another option, but I can't say that for certain.