I'm writing a cmdlet (script) on powershell and I wanted to use eunm as one of the parameters. But I don't know where to put enum definition so that it would be visible for cmdlet parameters declaration.
For example, I have a parameters definition of the script like this
[cmdletbinding()]
param(
[Parameter(Mandatory=$True)]
[string]$Level
)
and an enum like this
enum LevelEnum { NC = 1; NML = 2; CS = 3 }
I can't replace [string]
with [LevelEnum]
in the parameter definition because script will fail to locate enum definition. And I can't put definition before cmdletbinding
, it's not allowed.
I know how to do it if that would've been a function, I know it can be solved using ValidateSet
, but I need to have integer values correstonding to enum options.
[ValidateSet('NC','NML','CS')]
But the question is, can I do the same for a cmdlet?
Thanks to everyone. I ended up with a combination of defferent answers.
[cmdletbinding()]
param(
[Parameter(Mandatory=$True)]
[ValidateSet('NC','NML','CS')]
[string]$Level
)
# Convert level from string to enum
enum PatchLevel { NC = 1; NML = 2; CS = 3 }
[PatchLevel]$l = $Level
# Use the numeric value
Write-Host $l.value__