powershellparameter-sets

How do set parameter dependencies


Up till now I was more or less avoiding parameter-sets in my PowerShell script as I find them very verbose and quiet difficult to implement for complex dependencies.
There are several similar questions and answers at StackOverflow but I can find an acceptable solution or workaround to my situation. The actual script is even more complex but this is were I get stuck:

Function Test-ParamSet {
    [CmdletBinding(DefaultParameterSetName='Param1')][OutputType([Object[]])]Param (

        [Parameter(ParameterSetName = 'Param1', Mandatory = $True)]
        [Parameter(ParameterSetName = 'Param1Switch', Mandatory = $True)]
        $Param1,

        [Parameter(ParameterSetName = 'Param2', Mandatory = $True)]
        [Parameter(ParameterSetName = 'Param2Switch', Mandatory = $True)]
        $Param2,

        [Parameter(ParameterSetName = 'Param3', Mandatory = $True)]
        $Param3,

        [Parameter(ParameterSetName = 'Param1Switch')]
        [Parameter(ParameterSetName = 'Param2Switch')]
        [Switch]$Switch1,

        [Parameter(ParameterSetName = 'Param1Switch')]
        [Parameter(ParameterSetName = 'Param2Switch')]
        [Switch]$Switch2
    )
    Write-Host $PsCmdlet.ParameterSetName
}

Parameter rules:

Meaning the following commands should produce an error:

Test-ParamSet
Test-ParamSet -Param3 'Test' -Switch1

And the following commands should be accepted:

Test-ParamSet -Param1 'Test'
Test-ParamSet -Param1 'Test' -Switch1
Test-ParamSet -Param2 'Test' -Switch1 -Switch2

The problem is with the following command:

Test-ParamSet -Param2 'Test'

It generates an unexpected error:

Test-ParamSet : Parameter set cannot be resolved using the specified named parameters. 
One or more parameters issued cannot be used together or an insufficient number of parameters were provided.
At line:1 char:1
+ Test-ParamSet -Param2 'Test'
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidArgument: (:) [Test-ParamSet], ParameterBindingException
+ FullyQualifiedErrorId : AmbiguousParameterSet,Test-ParamSet

I guess this is related to this github issue mentioned by @mklement0 in How do you change a parameter's throw message?.

But how can I adequately resolve or workaround this using parameter-sets?


Solution

  • The switch parameters should be member of the Param1 and Param2 naming set. Then, the Param1Switch & Param2Switch naming set can be removed from $Param1 & $Param2 definition.

    Here's the end result.

    Function Test-ParamSet {
        [CmdletBinding(DefaultParameterSetName='Param1')][OutputType([Object[]])]Param (
    
            [Parameter(ParameterSetName = 'Param1', Mandatory = $True)]
            $Param1,
    
            [Parameter(ParameterSetName = 'Param2', Mandatory = $True)]
            $Param2,
    
            [Parameter(ParameterSetName = 'Param3', Mandatory = $True)]
            $Param3,
    
            [Parameter(ParameterSetName = 'Param1')]
            [Parameter(ParameterSetName = 'Param2')]
            [Switch]$Switch1,
    
            [Parameter(ParameterSetName = 'Param1')]
            [Parameter(ParameterSetName = 'Param2')]
            [Switch]$Switch2
        )
        Write-Host $PsCmdlet.ParameterSetName
    }