c# - PowerShell参数取值建议

标签 c# powershell

我用 C# 编写了一个 Cmdlet。是否可以为特定字符串参数(“PackageId”是此示例)提供所有可能的值?

public sealed class InstallPackageCommand : PSCmdlet
{
    [Parameter(Position = 0, Mandatory = true)]
    public string PackageId { get; set; }

    protected override void BeginProcessing()
    {
       //...
    }
} 

最佳答案

您可以在您的属性上为该参数使用 ValidateSetAttribute,例如:

[ValidateNotNullOrEmpty]
[ValidateSet(new string[] {"a","b","c"})]
[Parameter(Position = 0, Mandatory = true)]
public string PackageId { get; set; }

关于c# - PowerShell参数取值建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19709179/

相关文章:

powershell - 获取Export-VM的进度输出

c# - 在类内部时,调用它的私有(private)成员还是公共(public)属性更好?

c# - Visual Studio 2017 - 表达式交互发生了什么?

powershell - 安全移除 USB 驱动器

c# - 如何将 PSCredential 对象从 C# 代码传递到 Powershell 函数

powershell - 如何在 Powershell Active Directory cmdlet 中引用对象的属性?

powershell - 使用 PowerShell 在注册表项和值中搜索字符串

c# - 需要从类库读取值App.config

c# - 在 C# 中检测 wifi 连接

c# - 如何在覆盖 GetHashCode() 的类型上使用 Object.GetHashCode()