如何在PowerShell中定义多组互斥参数?

Ale*_*vis 1 parameters powershell

如果有多个(互斥参数)组,有没有办法让 PowerShell 强制执行互斥参数?

例如,考虑脚本中的这些参数Test.ps1(都是可选的和非位置的):

Param(
    # GROUP A (a1 and a2 are mutually exclusive)
    [switch]$a1,
    [switch]$a2,

    # GROUP B (b1 and b2 are mutually exclusive)
    [switch]$b1,
    [switch]$b2
)
Run Code Online (Sandbox Code Playgroud)

有没有一种方法可以强制指定最多来自 A 组(即$a1$a2)的一个参数和/或最多来自 B 组(即$b1$b2)的一个参数?

以下调用将是有效的

.\Test.ps1
.\Test.ps1 -a1
.\Test.ps1 -a2
.\Test.ps1 -b1
.\Test.ps1 -b2
.\Test.ps1 -a1 -b1
.\Test.ps1 -a1 -b2
.\Test.ps1 -a2 -b1
.\Test.ps1 -a2 -b2
Run Code Online (Sandbox Code Playgroud)

以下调用无效

.\Test.ps1 -a1 -a2 -b1 -b2
.\Test.ps1 -a1 -b1 -b2
.\Test.ps1 -a2 -b1 -b2
.\Test.ps1 -a1 -a2 -b1
.\Test.ps1 -a1 -a2 -b2
Run Code Online (Sandbox Code Playgroud)

我知道如何使用ParameterSetName来强制执行一组互斥参数,但我无法弄清楚如何强制执行多个组。甚至不确定这是否可能。请注意,实际示例比上面的示例更复杂(每组有多个参数,它们并非都是开关)。

更新:根据评论中的建议,我添加了参数集:

Param(
    # GROUP A (a1 and a2 are mutually exclusive)
    [Parameter(ParameterSetName="a1")]
    [Parameter(ParameterSetName="b1")]
    [Parameter(ParameterSetName="b2")]
    [switch]$a1,

    [Parameter(ParameterSetName="a2")]
    [Parameter(ParameterSetName="b1")]
    [Parameter(ParameterSetName="b2")]
    [switch]$a2,

    # GROUP B (b1 and b2 are mutually exclusive)
    [Parameter(ParameterSetName="a1")]
    [Parameter(ParameterSetName="a2")]
    [Parameter(ParameterSetName="b1")]
    [switch]$b1,

    [Parameter(ParameterSetName="a1")]
    [Parameter(ParameterSetName="a2")]
    [Parameter(ParameterSetName="b2")]
    [switch]$b2
)
Run Code Online (Sandbox Code Playgroud)

这似乎没有做我需要它做的事情:

PS D:\Scripts> Get-Help .\Test.ps1
Test.ps1 [-a1] [-a2] [-b2] [<CommonParameters>]
Test.ps1 [-a1] [-a2] [-b1] [<CommonParameters>]
Test.ps1 [-a1] [-b1] [-b2] [<CommonParameters>]
Test.ps1 [-a2] [-b1] [-b2] [<CommonParameters>]

rok*_*aru 6

您可以通过设置“Mandatory”来解决“ParameterSet”

function paramtest
{
    <# .Synopsis #>
    [CmdletBinding(DefaultParameterSetName = "default")]
    Param(
        # GROUP A (a1 and a2 are mutually exclusive)
        [Parameter(ParameterSetName="a1")]
        [Parameter(Mandatory, ParameterSetName="a1b1")]
        [Parameter(Mandatory, ParameterSetName="a1b2")]
        [switch]$a1,

        [Parameter(ParameterSetName="a2")]
        [Parameter(Mandatory, ParameterSetName="a2b1")]
        [Parameter(Mandatory, ParameterSetName="a2b2")]
        [switch]$a2,

        # GROUP B (b1 and b2 are mutually exclusive)
        [Parameter(ParameterSetName="b1")]
        [Parameter(Mandatory, ParameterSetName="a1b1")]
        [Parameter(Mandatory, ParameterSetName="a2b1")]
        [switch]$b1,

        [Parameter(ParameterSetName="b2")]
        [Parameter(Mandatory, ParameterSetName="a1b2")]
        [Parameter(Mandatory, ParameterSetName="a2b2")]
        [switch]$b2
    )

    "ParameterSetName is {0}" -f $PSCmdlet.ParameterSetName
}
Run Code Online (Sandbox Code Playgroud)

结果Get-Help如下。

PS > help paramtest | foreach syntax

paramtest [<CommonParameters>]

paramtest -a1 -b2 [<CommonParameters>]

paramtest -a1 -b1 [<CommonParameters>]

paramtest [-a1] [<CommonParameters>]

paramtest -a2 -b2 [<CommonParameters>]

paramtest -a2 -b1 [<CommonParameters>]

paramtest [-a2] [<CommonParameters>]

paramtest [-b1] [<CommonParameters>]

paramtest [-b2] [<CommonParameters>]
Run Code Online (Sandbox Code Playgroud)