【问题标题】:How to define multiple groups of mutually exclusive parameters in PowerShell?如何在PowerShell中定义多组互斥参数?
【发布时间】:2019-03-13 19:04:29
【问题描述】:

如果有多个(互斥参数)组,是否有办法让 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
)

有没有办法强制指定 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

以下调用将是无效

.\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

我知道如何使用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
)

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

PS D:\Scripts> 获取帮助 .\Test.ps1 Test.ps1 [-a1] [-a2] [-b2] [] Test.ps1 [-a1] [-a2] [-b1] [] Test.ps1 [-a1] [-b1] [-b2] [] Test.ps1 [-a2] [-b1] [-b2] []

【问题讨论】:

  • 创建多个参数集有什么问题?是的,可能会有很多系列,但这可能是最好的方法。
  • 我想看一个例子,因为我不能让它与多个集合一起工作。也许我做得不对,但在这个例子中,每个参数都有自己的集合和不是组成员的集合,比如 $a1: a1,b1,b2, $a2:a2,b1,b2, $b1 :b1,a1,a1 和 $b2:b2,a1,a2,对吗?如果是这样,它不起作用。
  • 你明白我的意思吗?还是我做得不对?
  • 我将用示例更新原始帖子。
  • 重新审视您的要求我认为这不能通过参数集来完成。虽然可以通过添加另一个(空)参数集并将其设置为默认值来实现不带参数调用脚本的要求,但使用参数的任意组合运行脚本的要求使得参数集方法不可行,b/c 参数集变得难以区分。您需要在这里使用dynamic parameters

标签: powershell parameters


【解决方案1】:

可以通过设置“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
}

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>]

【讨论】:

  • 您应该添加Get-Help paramtest 的输出以显示参数集的正确列表。
  • @rokumaru:你成就了我的一天!谢谢
猜你喜欢
  • 1970-01-01
  • 2013-03-09
  • 2014-02-03
  • 2016-02-18
  • 1970-01-01
  • 1970-01-01
  • 2016-05-04
  • 2015-10-19
  • 2012-05-03
相关资源
最近更新 更多