3

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

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

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

3 回答 3

5

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

[ValidateNotNullOrEmpty]
[ValidateSet(new string[] {"a","b","c"})]
[Parameter(Position = 0, Mandatory = true)]
public string PackageId { get; set; }
于 2013-10-31T15:41:22.847 回答
0

您可以创建一个客户枚举:

http://blogs.msdn.com/b/powershell/archive/2007/01/23/how-to-create-enum-in-powershell.aspx

然后使您的变量为枚举类型。

这也将使可能的值与来自命令 lilne 的 Powershell 选项卡完成一起工作。

于 2013-10-31T14:33:29.537 回答
0

自定义枚举示例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Management.Automation;

namespace ExampleNameSpace
{
    [Cmdlet(VerbsCommon.Get, "Something")]
    [OutputType("PSCustomObject")]
    public class GetSomething : PSCmdlet
    {
        public enum ExampleEnum { A, B, C };

        [Parameter(
            HelpMessage = "Enter A, B, or C.",
            Mandatory = true,
            Position = 0,
            ValueFromPipelineByPropertyName = true,
            ValueFromPipeline = true
        )]
        public ExampleEnum ExampleParameter { get; set; }

        protected override void ProcessRecord()
        {
            WriteObject(ExampleParameter);
            switch (ExampleParameter)
            {
                case ExampleEnum.A:
                    WriteObject("Case A");
                    break;
                case ExampleEnum.B:
                    WriteObject("Case B");
                    break;
                case ExampleEnum.C:
                    WriteObject("Case C");
                    break;
                default:
                    break;
            }
        }
    }
}

用法示例#1:

$ Get-Something

cmdlet Get-Something at command pipeline position 1
Supply values for the following parameters:
(Type !? for Help.)
ExampleParameter: !?
Enter A, B, or C.
ExampleParameter: a
A
Case A

用法示例 #2:

$ Get-Something -ExampleParameter x
Get-Something : Cannot bind parameter 'ExampleParameter'. Cannot convert value "x" to type
"ExampleNameSpace.GetSomething+ExampleEnum". Error: "Unable to match the identifier name x to a valid enumerator name.
Specify one of the following enumerator names and try again:
A, B, C"
At line:1 char:33
+ Get-Something -ExampleParameter x
+                                 ~
    + CategoryInfo          : InvalidArgument: (:) [Get-Something], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,ExampleNameSpace.GetSomething

用法示例#3:

$ "a","b","c","d" | Get-Something
A
Case A
B
Case B
C
Case C
Get-Something : The input object cannot be bound to any parameters for the command either because the command does not
take pipeline input or the input and its properties do not match any of the parameters that take pipeline input.
At line:1 char:20
+ "a","b","c","d" | Get-Something
+                    ~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (d:String) [Get-Something], ParameterBindingException
    + FullyQualifiedErrorId : InputObjectNotBound,ExampleNameSpace.GetSomething
于 2015-10-09T14:04:00.353 回答