#Powershell 5 anyway to import C# enum prior 2 param of the script file (without converting to func)?

18 messages · Page 1 of 1 (latest)

tardy hull
#

I've tried using module and it does not seem to import enums

Trying to import the enum for param validation, but also allow the script users (admins) to tab-complete options

wanton imp
tardy hull
#

I'm using it that way for automation purposes because then the admins don't have to run the script then run the function or install the module then run the function, because its use is temporary for building virtual machine images so they need to be able to copy it over and run it, and I'd like to autovalidate the fields for them using enums for code upkeep purposes

#

and for keeping the main script a little more abstract

wanton imp
#

if you import types, it should be a module

fringe juniper
#

Scripts are pretty limited in this respect. If you could make your script a module instead it'd be no problem at all.

ancient orchid
#

ValidateSet is your friend.

wanton imp
ancient orchid
#

You cannot, sadly, put an expression in an attribute

#

I tried something like this once

#
function Test {
  [CmdletBinding()]
  param(
    [ArgumentCompletions({1..14|%{"A$_"}})]
    $A
  )
}
#

Can you guess what the first thing in tab completion for Test -A {TAB} is?

#

However ... you could do that as an argument completer...

#

omg, it works

#
[CmdletBinding()]
param(
    [ArgumentCompleter({
        [Enum]::GetNames([Vowels])
    })]
    [string]$Vowel
)
enum Vowels {
    A = 1
    E = 2
    I = 3
    O = 4
    U = 5
}

$PSBoundParameters
#

If you put that in a file, you can tab complete it without running it first

#

Kind of scary that it works, honestly 😛

wanton imp