【发布时间】:2020-11-26 01:36:08
【问题描述】:
我在 C# 中使用 System.Management.Automation 命名空间,以便在 Powershell 模块中公开我的类库。在某些 cmdlet 中,我想提供一个行为类似于开关的可选参数(即,如果包含它,则该参数设置为 true)。我已经看到其他 Powershell Cmdlet 中的参数以这种方式运行,但我还没有看到使用 C# 接口完成此操作的方法。我能来的最接近的是接受布尔值的参数:
[Parameter(HelpMessage = "If enabled, no confirmation prompt will appear")]
public bool Quiet { get; set; } = false;
但是在执行中,参数本身是不能接受的,它需要带有一个值:
New-Widget -Path C:\temp\myfile.abc -Quiet $True
如果可能的话,我希望 Quiet 能够通过参数设置为 true:
New-Widget -Path C:\temp\myfile.abc -Quiet
这可能吗?
【问题讨论】:
标签: c# powershell