简体   繁体   English

PowerShell中的多个参数集

[英]Multiple parameter sets in PowerShell

Is it possible to use multiple sets to make two sets of parameters exclusive of each other, but inclusive with the other two sets? 是否可以使用多个集合使两组参数互相排斥,但包含其他两组参数? For example, say I had four parameters: 例如,假设我有四个参数:

param(
    $param1,
    $param2,
    $param3,
    $param4,
)

Param1 and 2 are exclusive of each other, but can be called whether 3 or 4 are called. Param1和2彼此独占,但可以调用3或4。 Similarly, param3 and 4 are exclusive of each other, but can be called whether 1 or 2 are called. 类似地,param3和4彼此排斥,但是可以调用1或2。 These are the okay sets of parameters that can be called together: 这些是可以一起调用的正常参数集:

./MyScript.ps1 -param1 -param3
./MyScript.ps1 -param1 -param4
./MyScript.ps1 -param2 -param3
./MyScript.ps1 -param2 -param4

These shouldn't be called at the same time: 不应同时调用这些:

./MyScript.ps1 -param1 -param2
./MyScript.ps1 -param1 -param3 -param4
./MyScript.ps1 -param2 -param3 -param4
./MyScript.ps1 -param3 -param1 -param2
./MyScript.ps1 -param4 -param1 -param2
./MyScript.ps1 -param3 -param4

I've tried a few things, but all of them seem to cause weird output, and still allow me to call what I would think to be "illegal" parameter sets. 我已经尝试过一些东西,但是所有这些东西似乎都会导致奇怪的输出,并且仍然允许我调用我认为是“非法”参数集的东西。 Here is an example of the parameter sets as I set them up: 以下是我设置参数集的示例:

[Parameter(ParameterSetName="onethree",Mandatory=$true,HelpMessage="a or b")]
[Parameter(ParameterSetName="onefour",Mandatory=$true,HelpMessage="a or b")]
[ValidateSet("a","b")]
[string]$one,
[Parameter(ParameterSetName="twothree",Mandatory=$true)]
[Parameter(ParameterSetName="twofour",Mandatory=$true)]
[switch]$two,
[Parameter(ParameterSetName="onethree")]
[Parameter(ParameterSetName="twothree")]
[string[]]$PropertyClass,
[Parameter(ParameterSetName="onefour")]
[Parameter(ParameterSetName="twofour")]
[string[]]$PropertyName

The script executes, but it doesn't complete correctly (If I call it with $two, it tries doing something with $one and causes the script to fail). 该脚本执行,但它没有正确完成(如果我用$ 2调用它,它会尝试用$ 1做一些事情并导致脚本失败)。 If I tab through the possible parameters, I don't see $two; 如果我通过可能的参数选项卡,我看不到$ 2; but if I type -t and hit [tab], it will autocomplete to -two . 但是如果我输入-t并点击[tab],它将自动完成到-two Any ideas what could be causing this? 可能导致这种情况的任何想法? Is it possible to do what I'm looking for? 有可能做我正在寻找的事情吗?

Use Get-Help 使用Get-Help

The best way to see what the parameter sets are is to use Get-Help My-Function . 查看参数集的最佳方法是使用Get-Help My-Function I created a function called Test-Sets with your given param() block, and this is the output from Get-Help : 我用你给定的param()块创建了一个名为Test-Sets的函数,这是Get-Help的输出:

SYNTAX
    Test-Sets -one <string> {a | b} [-PropertyName <string[]>]  [<CommonParameters>]

    Test-Sets -one <string> {a | b} [-PropertyClass <string[]>]  [<CommonParameters>]

    Test-Sets -two [-PropertyName <string[]>]  [<CommonParameters>]

    Test-Sets -two [-PropertyClass <string[]>]  [<CommonParameters>]

This looks like it satisfies your requirements, unless you always want 2 parameters, in which case I think the only thing you need to do is make $PropertyClass and $PropertyName mandatory in each of their 2 parameter sets. 这看起来满足您的要求,除非您总是需要2个参数,在这种情况下,我认为您唯一需要做的就是在其2个参数集中的每一个中强制使用$PropertyClass$PropertyName

If I'm missing something here, please clarify and include the failing command as @mike z requested. 如果我在这里遗漏了一些内容,请澄清并包含失败的命令,如@mike z所要求的那样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM