#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)
If you are running a .ps1 file like a function, It will not be possible without using a dynamic parameter block.
I don't recommend it.
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
if you import types, it should be a module
Scripts are pretty limited in this respect. If you could make your script a module instead it'd be no problem at all.
ValidateSet is your friend.
[ValidateSet($($null=Import-Module -Path 'mypath', [enum])]???
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 😛
Yeah well the script gets executed last so that makes sense...