PowerShell中类似枚举的开关参数

PowerShell中类似枚举的开关参数,powershell,Powershell,我正以这种方式在PowerShell脚本中使用开关参数 param( [switch] $Word, [switch] $Excel, [switch] $powerpoint, [switch] $v2007, [switch] $v2010, [switch] $x86, [switch] $x64, ) 我正试图找出任何简洁的方法使它更具枚举风格。 正如任何人可能猜到的,我希望用户在word、excel和powerpoint之间进行

我正以这种方式在PowerShell脚本中使用开关参数

param(
    [switch] $Word,
    [switch] $Excel,
    [switch] $powerpoint,
    [switch] $v2007,
    [switch] $v2010,
    [switch] $x86,
    [switch] $x64,
)
我正试图找出任何简洁的方法使它更具枚举风格。 正如任何人可能猜到的,我希望用户在word、excel和powerpoint之间进行选择。 在x2007和v2010之间

有没有一种简洁的方法来获取输入参数枚举样式

我对PowerShell是新手。因此,如果这听起来像是我不知道什么明显的事情,那么请给我指一些链接,我可以在那里阅读它。

这定义了如何在PowerShell 1.0中做到这一点。在PowerShell 2.0中,您可以使用如下添加类型:

C:\PS> Add-Type -TypeDefinition @'
>> public enum MyEnum {
>> A,
>> B,
>> C,
>> D
>> }
>> '@
>>
更新:以下是如何使用枚举:

C:\PS> function foo([MyEnum]$enum) { $enum }
C:\PS> foo ([MyEnum]::A)
A
您需要在参数周围加上括号才能将参数作为类型进行解析。这是必需的,因为参数或多或少被视为字符串。知道了这一点,您还可以以简单的字符串形式向它们传递enum,powershell会解决这个问题:

C:\PS> foo A
A
C:\PS> $arg = "B"
C:\PS> foo $arg
B
C:\PS> foo F
error*

错误-F不是枚举值之一-有效值包括A、B、C、D*

您可以使用
ValidateSet
属性:

function My-Func
{
    param (
        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [ValidateSet('Word', 'Excel', 'PowerPoint', 'v2007', 'v2010', 'x86', 'x64')]
        [String]$MyParam
    )

    Write-Host "Performing action for $MyParam"
}

My-Func -MyParam 'Word'
My-Func -MyParam 'v2007'
My-Func -MyParam 'SomeVal'
输出:

Performing action for Word
Performing action for v2007
My-Func : Cannot validate argument on parameter 'MyParam'. The argument "SomeVal" does not belong to the set "Word,Excel,PowerPoint,v2007,v2010,x86,x64" specified by the ValidateSet attribute. Supply an argument that is in the
 set and then try the command again.
At C:\Users\George\Documents\PowerShell V2\ValidateSetTest.ps1:15 char:17
+ My-Func -MyParam <<<<  'SomeVal'
    + CategoryInfo          : InvalidData: (:) [My-Func], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,My-Func
为Word执行操作
执行v2007的操作
My Func:无法验证参数“MyParam”上的参数。参数“SomeVal”不属于ValidateSet属性指定的集合“Word、Excel、PowerPoint、v2007、v2010、x86、x64”。提供位于
设置,然后重试该命令。
位于C:\Users\George\Documents\PowerShell V2\ValidateSetTest.ps1:15 char:17

+myfunc-MyParam我将使用
ValidateSet
parameter属性

发件人:

ValidateSet属性指定一组有效的值 参数或变量。如果出现以下情况,Windows PowerShell将生成错误: 参数或变量值与集合中的值不匹配

示例函数:

function test-value
{
    param(
        [Parameter(Position=0)]
        [ValidateSet('word','excel','powerpoint')]
        [System.String]$Application,

        [Parameter(Position=1)]
        [ValidateSet('v2007','v2010')]
        [System.String]$Version
    )


    write-host "Application: $Application"
    write-host "Version: $Version"
}   


PS > test-value -application foo
输出:

Performing action for Word
Performing action for v2007
My-Func : Cannot validate argument on parameter 'MyParam'. The argument "SomeVal" does not belong to the set "Word,Excel,PowerPoint,v2007,v2010,x86,x64" specified by the ValidateSet attribute. Supply an argument that is in the
 set and then try the command again.
At C:\Users\George\Documents\PowerShell V2\ValidateSetTest.ps1:15 char:17
+ My-Func -MyParam <<<<  'SomeVal'
    + CategoryInfo          : InvalidData: (:) [My-Func], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,My-Func

测试值:无法验证参数“Application”上的参数。参数“foo”不属于ValidateSet属性指定的集合“word、excel、powerpoint”。请提供集合中的参数,然后重试该命令。

由于PowerShell 5,您实际上可以在本机使用/创建
枚举

enum OS {
    Windows
    Linux
    iOS
}
然后,这也可以作为其类型显示

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     OS                                       System.Enum

可能是一个有用的链接。

但我如何使用这些枚举示例来实现命令行“param”目的?一个有趣的示例,但我认为在这种情况下它并没有真正的用处。是的,必须使用parens类型的比特。OTOH我喜欢制表符完成——仅次于枚举值的真正静态验证。ValidateSet当然是语言中的一个不错的功能,可惜PowerShell没有使用该信息来驱动选项卡完成。顺便说一句,如果只是一组选择,你可以像OP一样使用开关-只需将每个开关放在单独的参数集中,然后选择一个作为默认值(如果没有指定)。不管它值多少,我认为值得一提的是,使用真正的enum允许Get-Help为脚本用户提供帮助,而不会使其成为不透明的运行时雷区。在这里的示例中,“获取帮助”将显示:验证集成员的语法foo-enum{A | B | C | D}制表符补全现在似乎可以工作了,因为我们将在将来使用。这种方法迫使您使用高级函数,这并不总是可取的。您也不会在枚举值上获得任何形式的自动完成。o您不必将枚举计算放在参数中,例如
[MyEnum]::A
。所以,我猜是一个混合包。:-)谢谢你,基思。另一方面,函数需要执行各种参数检查,以确定是否指定了多个开关参数。顺便说一句,我们不是都在使用PowerShell v2:)Keith,如果您使用PowerTab,您将在ValidateSet上获得选项卡扩展。。。就这么说吧现在,您确实获得了集合的选项卡扩展(在Windows 8.1中测试,在Powershell 3.0和4.0中测试,但在2.0中测试不到)