0

考虑这个 Cmdlet(删除使用):

public class Foo : Cmdlet
{
    [Parameter(Mandatory = true, ParameterSetName = "Foo"]
    public string A { get; set; }

    [Parameter(Mandatory = true, ParameterSetName = "Bar"]
    public string B { get; set; }

    [Parameter(Mandatory = true, ParameterSetName = "Bar"]
    public string C { get; set; }

    protected override void ProcessRecord()
    {
         /* magic goes here */
    }
}

现在您可以执行此 Cmdlet 的方式如下:

# like this
Foo

# or
Foo -A "test"

# or
Foo -B "test" -C "test"

拥有 and A 和 B 不起作用,仅使用 B 也不起作用(需要 C)。

所以这很好,但是我想防止你可以通过键入来执行 Cmdlet Foo,现在我可以在代码中检查它,但是由于 Powershell 非常棒,有没有办法通过运行时强制执行它?

4

1 回答 1

3

DefaultParameterSetName您可以通过属性的属性指示 PowerShell 默认为命名参数集CmdletAttribute

[Cmdlet("Foo", "Bar", DefaultParameterSetName = "Foo")]
public class Foo : Cmdlet
{
   // ...
}
于 2013-10-31T08:54:33.493 回答