枚举类似于PowerShell中的switch参数

bit*_*its 56 powershell

我以这种方式在PowerShell脚本中使用switch参数.

param(
    [switch] $Word,
    [switch] $Excel,
    [switch] $powerpoint,
    [switch] $v2007,
    [switch] $v2010,
    [switch] $x86,
    [switch] $x64,
)
Run Code Online (Sandbox Code Playgroud)

我试图弄清楚任何整洁的方式让它更多的枚举风格.正如任何人可能猜到的那样,我希望用户在word,excel和powerpoint之间进行选择.在x2007和v2010之间.

有没有一个简洁的方法来获得输入参数枚举风格?

我是PowerShell的新手.所以,如果这听起来像我不知道一些明显的东西,那么请指出我可以阅读的一些链接.

Sha*_*evy 95

我会改用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
Run Code Online (Sandbox Code Playgroud)

输出:

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.

  • 你现在可以获得套件的标签扩展(在Windows 8.1中,在Powershell 3.0和4.0中进行测试,但不是2.0) (3认同)

Geo*_*rth 11

您可以使用以下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'
Run Code Online (Sandbox Code Playgroud)

输出:

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
Run Code Online (Sandbox Code Playgroud)


Kei*_*ill 10

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

C:\PS> Add-Type -TypeDefinition @'
>> public enum MyEnum {
>> A,
>> B,
>> C,
>> D
>> }
>> '@
>>
Run Code Online (Sandbox Code Playgroud)

更新:以下是如何使用枚举:

C:\PS> function foo([MyEnum]$enum) { $enum }
C:\PS> foo ([MyEnum]::A)
A
Run Code Online (Sandbox Code Playgroud)

您需要围绕参数的括号将参数解析为Type.这是必需的,因为参数被或多或少地视为字符串.知道了这一点,你也可以用一个简单的字符串形式传递它们,并且PowerShell会弄明白:

C:\PS> foo A
A
C:\PS> $arg = "B"
C:\PS> foo $arg
B
C:\PS> foo F
error*
Run Code Online (Sandbox Code Playgroud)

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

  • 验证集成员的选项卡完成现在似乎可以工作,因为我们在未来。 (2认同)

Ale*_*x_P 7

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

enum OS {
    Windows
    Linux
    iOS
}
Run Code Online (Sandbox Code Playgroud)

这也作为其类型可见。

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     OS                                       System.Enum
Run Code Online (Sandbox Code Playgroud)

也许4sysops提供的有用链接。

要在函数中使用它,我可以创建一个单独的.psm1文件并添加您需要的所有枚举 - 请参阅我的评论,它引用了一个类似的问题 - 或者您可以在文件顶部设置此枚举.psm1并在同一文件中使用。

例如,我创建了该文件test.psm1并添加了以下代码:

enum OS {
    Windows
    Linux
    iOS
}

function Get-OS {
    param (
        [Parameter(Mandatory)]
        [OS]$os
    )

    Write-Host -Object "The operating system is $os."
    
}
Run Code Online (Sandbox Code Playgroud)

我导入文件Import-Module .\test.psm1并运行它:

PS > Get-OS -os Linux
The operating system is Linux.
Run Code Online (Sandbox Code Playgroud)