互斥powershell参数 脚本 我正在使用Visual Studio 2008和.NET 3.5为Powershell 2.0编写cmdlet cmdlet需要3个参数

互斥powershell参数 脚本 我正在使用Visual Studio 2008和.NET 3.5为Powershell 2.0编写cmdlet cmdlet需要3个参数,powershell,parameters,optional-parameters,Powershell,Parameters,Optional Parameters,我预期的cmdlet语法如下所示: cmdletname [foo|bar] p1, p2 这意味着用户必须为“-foo”或“-bar”提供一个值,但不能同时提供这两个值 有效输入示例 无效输入示例 我的问题 我的问题是如何在powershell中执行此操作,以便它为我执行所有检查—或者如果可能的话 我知道我可以使用foo和bar的两个可选参数,只需手动执行错误检查。这就是我目前实现它的方式 或者,我对不同方法的建议感兴趣 您可以使用来声明多个参数集。然后,只需将互斥的参数分配给不同的参

我预期的cmdlet语法如下所示:

cmdletname [foo|bar] p1, p2
  • 这意味着用户必须为“-foo”或“-bar”提供一个值,但不能同时提供这两个值
有效输入示例 无效输入示例 我的问题
  • 我的问题是如何在powershell中执行此操作,以便它为我执行所有检查—或者如果可能的话
  • 我知道我可以使用foo和bar的两个可选参数,只需手动执行错误检查。这就是我目前实现它的方式
  • 或者,我对不同方法的建议感兴趣
您可以使用来声明多个参数集。然后,只需将互斥的参数分配给不同的参数集

编辑:

这也记录在“ParameterSetName命名参数”部分下的“关于函数\u高级\u参数”中。这就是使用cmdlet处理不同参数集的方式,如
Get Random
(具有互斥参数):

参数
one
two
在不同的参数集中,因此不能一起指定:

> exclusive_params -one foo -two bar -three third
exclusive_params : Parameter set cannot be resolved using the specified named parameters.
At line:1 char:17
+ exclusive_params <<<<  -one foo -two bar -three third
    + CategoryInfo          : InvalidArgument: (:) [exclusive_params], ParameterBindingException
    + FullyQualifiedErrorId : AmbiguousParameterSet,exclusive_params
…或:

> exclusive_params -two bar -three third
one:
two: bar
three: third

下面是在中使用取自cmdlet的ParameterSetName的示例。顺便说一句,你的想法可以


请注意,cmdlet通常声明一个默认ParameterSetName,以帮助PowerShell在出现歧义时确定要使用的“默认”参数集。稍后,如果需要,您可以通过查询this.ParameterSetName来确定哪个参数集有效,就像上面在EndProcessing()覆盖中switch语句所做的那样。

我来到这里,但有一个附加要求:可选互斥参数

这篇文章帮我找到了一半答案。所以我想在这里发布完整的答案,以防有人有相同的要求

下面的代码可用于Powershell脚本的顶部,以具有4个可选参数,其中LaunchAsAdmin和LaunchAsCupOnBrowser是互斥的,而token和WorkstationName也是可选的,但可以与任何其他参数组合

[CmdletBinding(DefaultParametersetName="default")]                  
Param(
    [string]$token,
    [string]$WorkstationName,
    [parameter(ParameterSetName="seta")][switch]$LaunchAsAdmin,
    [parameter(ParameterSetName="setb")][switch]$LaunchAsCouponBrowser  
)

非常好,准确地回答了我的问题,我不知道为什么这一点没有得到更多的支持!在上面的建议中没有强调的是,除了将互斥参数移动到令牌和工作站参数下面之外,还需要添加CmdletBinding引用。否则,在执行没有参数的函数时,Powershell将给您带来可怕的“含糊不清的USParameterSet”错误。如果您对CmdletBinding行不感兴趣,Powershell希望所有参数都是“必需的”。
function exclusive_params() { 
    param( 
        [parameter(ParameterSetName="seta")]$one,
        [parameter(ParameterSetName="setb")]$two, 
        $three 
    )
    "one: $one"; "two: $two"; "three: $three" 
}
> exclusive_params -one foo -two bar -three third
exclusive_params : Parameter set cannot be resolved using the specified named parameters.
At line:1 char:17
+ exclusive_params <<<<  -one foo -two bar -three third
    + CategoryInfo          : InvalidArgument: (:) [exclusive_params], ParameterBindingException
    + FullyQualifiedErrorId : AmbiguousParameterSet,exclusive_params
> exclusive_params -one foo -three third
one: foo
two:
three: third
> exclusive_params -two bar -three third
one:
two: bar
three: third
[Cmdlet(VerbsCommon.Set, PscxNouns.Clipboard, 
        DefaultParameterSetName = ParamSetText)]
[Description("Copies the item in the system clipboard.")]
[RelatedLink(typeof(GetClipboardCommand))]
[RelatedLink(typeof(OutClipboardCommand))]
[RelatedLink(typeof(WriteClipboardCommand))]
public class SetClipboardCommand : ClipboardCommandBase
{
    ... fields elided

    const string ParamSetRtf = "Rtf";
    const string ParamSetHtml = "Html";
    const string ParamSetText = "Text";
    const string ParamSetFiles = "Files";
    const string ParamSetImage = "Image";
    . 
    [AllowNull]
    [Parameter(ValueFromPipeline = true, ParameterSetName = ParamSetImage)]
    public Image Image { get; set; }
    . 
    [AllowNull]
    [AllowEmptyCollection]
    [Parameter(ValueFromPipeline = true, ValueFromRemainingArguments = true,
               ParameterSetName = ParamSetFiles)]
    public FileSystemInfo[] Files { get; set; }
    . 
    [AllowNull]
    [AllowEmptyString]
    [Parameter(ValueFromPipeline = true, ValueFromRemainingArguments = true,
               ParameterSetName = ParamSetText)]
    public string Text { get; set; }
    . 
    [Parameter(ValueFromPipeline = true, ValueFromRemainingArguments = true,
               ParameterSetName = ParamSetHtml)]
    public string Html { get; set; }
    .         
    [Parameter(ValueFromPipeline = true, ValueFromRemainingArguments = true,
               ParameterSetName = ParamSetRtf)]
    public string Rtf { get; set; }
    . 
    protected override void ProcessRecord()
    {
        ...
    }
    .
    protected override void EndProcessing()
    {
        ExecuteWrite(delegate
        {
            switch (ParameterSetName)
            {
                case ParamSetFiles:
                    if (Paths.Count == 0)
                        WinFormsClipboard.Clear();
                    else
                        WinFormsClipboard.SetFileDropList(_paths);
                    break;

                case ParamSetImage:
                    if (Image == null)
                        WinFormsClipboard.Clear();
                    else
                        WinFormsClipboard.SetImage(_image);
                    break;

                case ParamSetRtf:
                    SetTextContents(Rtf, TextDataFormat.Rtf);
                    break;

                case ParamSetHtml:
                    SetTextContents(Html, TextDataFormat.Html);
                    break;

                default:
                    SetTextContents(Text, TextDataFormat.UnicodeText);
                    break;
            }
        });
    }
    ...
}
[CmdletBinding(DefaultParametersetName="default")]                  
Param(
    [string]$token,
    [string]$WorkstationName,
    [parameter(ParameterSetName="seta")][switch]$LaunchAsAdmin,
    [parameter(ParameterSetName="setb")][switch]$LaunchAsCouponBrowser  
)