繁体   English   中英

PowerShell 中的枚举类开关参数

[英]Enum like switch parameter in PowerShell

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

param(
    [switch] $Word,
    [switch] $Excel,
    [switch] $powerpoint,
    [switch] $v2007,
    [switch] $v2010,
    [switch] $x86,
    [switch] $x64,
)

我试图找出任何巧妙的方法来让它更具枚举风格。 任何人都可能猜到,我希望用户在 word、excel 和 powerpoint 之间进行选择。 在 x2007 和 v2010 之间。

有没有一种巧妙的方法来获取输入参数枚举样式?

我是 PowerShell 的新手。 因此,如果这听起来像我不知道什么明显的东西,那么请指向我可以阅读的链接。

我会改用ValidateSet参数属性。

来自: about_Functions_Advanced_Parameters

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

输出:

test-value : Cannot validate argument on parameter 'Application'. The argument "foo" does not belong to the set "word,excel,powerpoint" specified by the ValidateSet attribute. Supply an argument that is in the set and then try the command again.

您可以使用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

PowerShell 团队的这篇博文定义了如何在 PowerShell 1.0 中执行此操作。 在 PowerShell 2.0 中,您可以像这样使用 Add-Type:

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

您需要参数周围的括号才能将参数解析为类型。 这是必需的,因为参数或多或少地被视为字符串。 知道了这一点,您还可以将枚举以简单的字符串形式传递给它们,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 *

从 PowerShell 5 开始,您实际上可以在本地使用/创建Enum

enum OS {
    Windows
    Linux
    iOS
}

然后这也可以作为其类型可见。

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

也许是4sysops的有用链接。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM