Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell enum parameters

[Parameter (Mandatory=$False)]
[ValidateSet("Val1", "Val2", "Val3", "Val4", "Val5",ignorecase=$true)]
[string[]] $configs = ""

Is there a way to alter the above so that I can accept several of the enum values in one go?

I'm hoping to be able to launch the script as so:

.\MyAwesome-Script.ps1 -config Val1 Val2 (or any combination of enum values as parameters)

But I need this to also be tab complete-able (is that even a word?)

For completeness, I'm using PS 4.0 and PSCX 3.1 is also installed

like image 892
SteveMustafa Avatar asked Jul 30 '26 12:07

SteveMustafa


1 Answers

Just make your variable an array and it'll work just fine. Given the following function:

function Test-ValidateSet
{
    PARAM(
        [ValidateSet("Val1", "Val2", "Val3")]
        [string[]]$MyParam
    )

    foreach($value in $MyParam)
    {
        Write-Host "Parameter given: $value"
    }
}

For the above method, I get tab-completion on the MyParam parameter. To enter an array for the parameter, just separate the values with comma characters.

Test-ValidateSet -MyParam Val1, Val2, Val3

This supports tab-completion in both the PowerShell console and the PowerShell ISE.

like image 53
Robert Westerlund Avatar answered Aug 01 '26 02:08

Robert Westerlund



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!