我们都知道PowerShell是基于NET Framework库的,自从PowerShell开始支持Class关键字后,我们也可以在PowerShell使用Enum枚举,我们先给出下面这个完整的小例子。
#requires -Version 5
enum MyFruit
{
Apple
Orange
Watermelon
}
function Select-Fruit
{
param
(
[MyFruit]
[Parameter(Mandatory=$true)]
$Fruit
)
"You like $Fruit"
}
Select-Fruit
PS C:\Users\Administrator> C:\Sample\Untitled4.ps1
cmdlet Select-Fruit at command pipeline position 1
Supply values for the following parameters:
Fruit: Apple
You like Apple
enum MyFruit
{
Apple
Orange
Watermelon
}
[MyFruit]
[Parameter(Mandatory=$true)]
$Fruit
我们可以简单修改下脚本,看看是否它是一个枚举:
#requires -Version 5
enum MyFruit
{
Apple
Orange
Watermelon
}
function Select-Fruit
{
param
(
[MyFruit]
[Parameter(Mandatory=$true)]
$Fruit
)
$Type = ($Fruit).GetType() | Select-Object -ExpandProperty BaseType
"The type is $Type"
}
Select-Fruit
得到结果如下,就是这样!
PS C:\Users\Administrator> C:\XMLSample\Untitled4.ps1
cmdlet Select-Fruit at command pipeline position 1
Supply values for the following parameters:
Fruit: apple
The type is System.Enum