简体   繁体   English

Powershell:参数集,需要一个或另一个

[英]Powershell: parametersets, require one or the other

Consider this Cmdlet (usings removed): 考虑以下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 */
    }
}

Now the way you can execute this Cmdlet is as follows: 现在,执行此Cmdlet的方式如下:

# like this
Foo

# or
Foo -A "test"

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

Having and A and B doesn't work, neither does only using B (requires C). 拥有和A和B无效,仅使用B也无效(需要C)。

So that's fine, however I want to prevent that you can execute the Cmdlet by just typing Foo , now I can check that in code, but since Powershell is so awesome, is there a way to enforce this through the runtime? 很好,但是我想防止您只键入Foo就可以执行Cmdlet,现在我可以在代码中进行检查了,但是由于Powershell非常强大,有没有办法在运行时强制执行此操作?

You may instruct PowerShell to default to a named parameter set, by way of the DefaultParameterSetName property of the CmdletAttribute attribute: 您可以通过CmdletAttribute属性的DefaultParameterSetName属性,指示PowerShell默认使用命名参数集:

[Cmdlet("Foo", "Bar", DefaultParameterSetName = "Foo")]
public class Foo : Cmdlet
{
   // ...
}

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

相关问题 当一个DLL文件引用另一个时,使用PowerShell中的两个DLL文件 - Using two DLL files from PowerShell when one DLL file references the other one 构造彼此需要的代表 - Constructing delegates that require each other 从其他Powershell同步运行PowerShell脚本 - Running powershell script from other powershell synchronically 有用的PowerShell 一内胆 - Useful PowerShell one liners FluentValidation 需要三个值之一 - FluentValidation require one out of three values Powershell:调用.NET函数是否总是需要括号吗? - Powershell: Does calling .NET a function always require parenthesis? 在PowerShell中将多个参数放入一个变量中 - Multiple parameters put into one variable in PowerShell asp.net项目中的LocalDb是否需要一些配置才能在其他计算机上运行? VS2012 - LocalDb in asp.net project require some configurations to run on other machine? VS2012 具有两个模板的模板化用户控件,如何在其他模板中查找需要控件ID的属性的控件 - Templated User Control with two templates how to find control in other template for properties which require control id “无法在计算机机器名称上打开服务控制管理器。 此操作可能需要其他权限” - “Cannot open Service Control Manager on computer machine name. This operation might require other privileges”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM