powershellparametersparameter-sets

Trouble with multiple parameter sets


I need to get three parameters for a function that I am creating. Parameter one must be in all parameter sets. The other two parameters has multiple permutations.

I tried playing around with the parameter sets and I've searched for other threads, but none of them seem to be discussing this scenario.

My question is how do I create the parameter sets so that there are only the following possibilities.

Test-ParametersNow -ParamZ <string> -ParamA <string> -Param1 <string>
Test-ParametersNow -ParamZ <string> -ParamA <string> -Param2 <string>
Test-ParametersNow -ParamZ <string> -ParamA <string> -Param3
Test-ParametersNow -ParamZ <string> -ParamA <string> -Param4
Test-ParametersNow -ParamZ <string> -ParamA <string> -Param5
Test-ParametersNow -ParamZ <string> -ParamB -Param1 <string>
Test-ParametersNow -ParamZ <string> -ParamB -Param2 <string>
Test-ParametersNow -ParamZ <string> -ParamB -Param3
Test-ParametersNow -ParamZ <string> -ParamB -Param4
Test-ParametersNow -ParamZ <string> -ParamB -Param5
Test-ParametersNow -ParamZ <string> -ParamC <string> -Param1 <string>
Test-ParametersNow -ParamZ <string> -ParamC <string> -Param2 <string>
Test-ParametersNow -ParamZ <string> -ParamC <string> -Param3
Test-ParametersNow -ParamZ <string> -ParamC <string> -Param4
Test-ParametersNow -ParamZ <string> -ParamC <string> -Param5

Here is my function.

function Test-ParametersNow{
[CmdletBinding()]
    param (
        [parameter(Mandatory=$true,Position = 0)][string]$ParamZ,


        [Parameter(ParameterSetName="Param1", Mandatory=$false)]
        [Parameter(ParameterSetName="Param2", Mandatory=$false)]
        [Parameter(ParameterSetName="Param3", Mandatory=$false)]
        [Parameter(ParameterSetName="Param4", Mandatory=$false)]
        [Parameter(ParameterSetName="Param5", Mandatory=$false)]
        [Parameter(ParameterSetName="ParamA", Mandatory=$true,Position = 1)][ValidateNotNullOrEmpty()][string]$ParamA,

        [Parameter(ParameterSetName="Param1", Mandatory=$false)]
        [Parameter(ParameterSetName="Param2", Mandatory=$false)]
        [Parameter(ParameterSetName="Param3", Mandatory=$false)]
        [Parameter(ParameterSetName="Param4", Mandatory=$false)]
        [Parameter(ParameterSetName="Param5", Mandatory=$false)]
        [Parameter(ParameterSetName="ParamB", Mandatory=$true,Position = 1)][switch]$ParamB,

        [Parameter(ParameterSetName="Param1", Mandatory=$false)]
        [Parameter(ParameterSetName="Param2", Mandatory=$false)]
        [Parameter(ParameterSetName="Param3", Mandatory=$false)]
        [Parameter(ParameterSetName="Param4", Mandatory=$false)]
        [Parameter(ParameterSetName="Param5", Mandatory=$false)]
        [Parameter(ParameterSetName="ParamC", Mandatory=$true,Position = 1)][ValidateNotNullOrEmpty()][string]$ParamC,

        [Parameter(ParameterSetName="ParamA", Mandatory=$false)]
        [Parameter(ParameterSetName="ParamB", Mandatory=$false)]
        [Parameter(ParameterSetName="ParamC", Mandatory=$false)]
        [Parameter(ParameterSetName="Param1", Mandatory=$true,Position = 2)][ValidateNotNullOrEmpty()][string]$Param1,

        [Parameter(ParameterSetName="ParamA", Mandatory=$true)]
        [Parameter(ParameterSetName="ParamB", Mandatory=$true)]
        [Parameter(ParameterSetName="ParamC", Mandatory=$true)]
        [Parameter(ParameterSetName="Param2", Mandatory=$true,Position = 2)][ValidateNotNullOrEmpty()][string]$Param2,

        [Parameter(ParameterSetName="ParamA", Mandatory=$true)]
        [Parameter(ParameterSetName="ParamB", Mandatory=$true)]
        [Parameter(ParameterSetName="ParamC", Mandatory=$true)]
        [Parameter(ParameterSetName="Param3", Mandatory=$true,Position = 2)][switch]$Param3,

        [Parameter(ParameterSetName="ParamA", Mandatory=$true)]
        [Parameter(ParameterSetName="ParamB", Mandatory=$true)]
        [Parameter(ParameterSetName="ParamC", Mandatory=$true)]
        [Parameter(ParameterSetName="Param4", Mandatory=$true,Position = 2)][switch]$Param4,

        [Parameter(ParameterSetName="ParamA", Mandatory=$true)]
        [Parameter(ParameterSetName="ParamB", Mandatory=$true)]
        [Parameter(ParameterSetName="ParamC", Mandatory=$true)]
        [Parameter(ParameterSetName="Param5", Mandatory=$true,Position = 2)][switch]$Param5
    )
}

#Show-Command Test-ParametersNow
Get-Help Test-ParametersNow

These are the results that I get from Get-Help Test-ParametersNow

NAME
    Test-ParametersNow

SYNTAX
    Test-ParametersNow [-ParamZ ] <string> [-ParamA] <string> -Param2 <string> -Param3 -Param4 -Param5 [-Param1 <string>]  [<CommonParameters>]

    Test-ParametersNow [-ParamZ ] <string> [-Param5] [-ParamA <string>] [-ParamB] [-ParamC <string>]  [<CommonParameters>]

    Test-ParametersNow [-ParamZ ] <string> [-Param4] [-ParamA <string>] [-ParamB] [-ParamC <string>]  [<CommonParameters>]

    Test-ParametersNow [-ParamZ ] <string> [-Param3] [-ParamA <string>] [-ParamB] [-ParamC <string>]  [<CommonParameters>]

    Test-ParametersNow [-ParamZ ] <string> [-Param2] <string> [-ParamA <string>] [-ParamB] [-ParamC <string>]  [<CommonParameters>]

    Test-ParametersNow [-ParamZ ] <string> [-Param1] <string> [-ParamA <string>] [-ParamB] [-ParamC <string>]  [<CommonParameters>]

    Test-ParametersNow [-ParamZ ] <string> [-ParamB] -Param2 <string> -Param3 -Param4 -Param5 [-Param1 <string>]  [<CommonParameters>]

    Test-ParametersNow [-ParamZ ] <string> [-ParamC] <string> -Param2 <string> -Param3 -Param4 -Param5 [-Param1 <string>]  [<CommonParameters>]


ALIASES
    None


REMARKS
    None

Solution

  • You have defined 8 different parameter sets (A,B,C,1,2,3,4,5), but since each of A, B, or C each has 5 options, it seems like you want 15: A1 thru A5, B1 thru B5, C1 thru C5. Each parameter (except Z), belongs to several of these sets as required. Like this:

    function Test-ParametersNow{
    [CmdletBinding()]
        param (
            [parameter(Mandatory=$true,Position = 0)][string]$ParamZ,
    
    
            [Parameter(ParameterSetName="ParamA1", Mandatory=$true,Position = 1)]
            [Parameter(ParameterSetName="ParamA2", Mandatory=$true,Position = 1)]
            [Parameter(ParameterSetName="ParamA3", Mandatory=$true,Position = 1)]
            [Parameter(ParameterSetName="ParamA4", Mandatory=$true,Position = 1)]
            [Parameter(ParameterSetName="ParamA5", Mandatory=$true,Position = 1)]
            [ValidateNotNullOrEmpty()][string]$ParamA,
    
            [Parameter(ParameterSetName="ParamB1", Mandatory=$true,Position = 1)]
            [Parameter(ParameterSetName="ParamB2", Mandatory=$true,Position = 1)]
            [Parameter(ParameterSetName="ParamB3", Mandatory=$true,Position = 1)]
            [Parameter(ParameterSetName="ParamB4", Mandatory=$true,Position = 1)]
            [Parameter(ParameterSetName="ParamB5", Mandatory=$true,Position = 1)]
            [switch]$ParamB,
    
            [Parameter(ParameterSetName="ParamC1", Mandatory=$true,Position = 1)]
            [Parameter(ParameterSetName="ParamC2", Mandatory=$true,Position = 1)]
            [Parameter(ParameterSetName="ParamC3", Mandatory=$true,Position = 1)]
            [Parameter(ParameterSetName="ParamC4", Mandatory=$true,Position = 1)]
            [Parameter(ParameterSetName="ParamC5", Mandatory=$true,Position = 1)]
            [ValidateNotNullOrEmpty()][string]$ParamC,
    
            [Parameter(ParameterSetName="ParamA1", Mandatory=$true,Position = 2)]
            [Parameter(ParameterSetName="ParamB1", Mandatory=$true,Position = 2)]
            [Parameter(ParameterSetName="ParamC1", Mandatory=$true,Position = 2)]
            [ValidateNotNullOrEmpty()][string]$Param1,
    
            [Parameter(ParameterSetName="ParamA2", Mandatory=$true,Position = 2)]
            [Parameter(ParameterSetName="ParamB2", Mandatory=$true,Position = 2)]
            [Parameter(ParameterSetName="ParamC2", Mandatory=$true,Position = 2)]
            [ValidateNotNullOrEmpty()][string]$Param2,
    
            [Parameter(ParameterSetName="ParamA3", Mandatory=$true,Position = 2)]
            [Parameter(ParameterSetName="ParamB3", Mandatory=$true,Position = 2)]
            [Parameter(ParameterSetName="ParamC3", Mandatory=$true,Position = 2)]
            [switch]$Param3,
    
            [Parameter(ParameterSetName="ParamA4", Mandatory=$true,Position = 2)]
            [Parameter(ParameterSetName="ParamB4", Mandatory=$true,Position = 2)]
            [Parameter(ParameterSetName="ParamC4", Mandatory=$true,Position = 2)]
            [switch]$Param4,
    
            [Parameter(ParameterSetName="ParamA5", Mandatory=$true,Position = 2)]
            [Parameter(ParameterSetName="ParamB5", Mandatory=$true,Position = 2)]
            [Parameter(ParameterSetName="ParamC5", Mandatory=$true,Position = 2)]
            [switch]$Param5
        )
    }