考虑这个 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 非常棒,有没有办法通过运行时强制执行它?