Enumerate switch option in PowerShell

I use switch options in my PowerShell script this way.

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

I am trying to find some neat way to make it more enum style. As you might guess, I would like the user to choose between the words excel and powerpoint. And between x2007 and v2010.

Is there a neat way to get the enum style input parameter?

I am new to PowerShell. Therefore, if it sounds like this, I do not know anything obvious, then please provide me with a link where I can read about it.

+43
powershell
Sep 17 '10 at 14:26
source share
3 answers

Instead, I would use the ValidateSet parameter attribute.

From: about_Functions_Advanced_Parameters

The ValidateSet attribute specifies the set of valid values ​​for a parameter or variable. Windows PowerShell generates an error if the value of a parameter or variable does not match the value in the set.

Function example:

 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 

Output:

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.

+74
Sep 17 '10 at 21:10
source share

This PowerShell blog post defines how to do this in PowerShell 1.0. In PowerShell 2.0, you can use Add-Type as follows:

 C:\PS> Add-Type -TypeDefinition @' >> public enum MyEnum { >> A, >> B, >> C, >> D >> } >> '@ >> 

Update: Here's how to use the enumeration:

 C:\PS> function foo([MyEnum]$enum) { $enum } C:\PS> foo ([MyEnum]::A) A 

You need parentheses around the argument to parse the argument as a type. This is necessary because arguments are treated more or less like strings. Knowing this, you can also pass them enum in simple string form, and powershell will figure it out:

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

error - F is not one of the listed values ​​- valid values ​​include A, B, C, D *

+7
Sep 17 '10 at 14:35
source share

You can use the ValidateSet attribute:

 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' 

Output:

 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 
+7
Sep 17 '10 at 21:10
source share



All Articles