自定义枚举示例:
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