one-script.ps1 looks like this:
. $PSScriptRoot\another-script.ps1
Param (
[Parameter(Mandatory = $True)]
[MyEnum]$enum
)
write-host "hi"
MyEnum is defined in another-script.ps1:
Add-Type -TypeDefinition @"
public enum MyEnum {
a, b, c, d
}
"@
Running one-script.ps1 -enum badValue prints the validation exception as expected, but doesn't stop and prints "hi" as well. The reason I assume is that the Param block can't be preceded by the import statement.
However if I move the import statement below the Param block, the script fails because MyEnum type is undefined.
So how can I use MyEnum class while also having the validation check failure stop script from executing?
One solution is to add MyEnum in user profile, but it's not a good general solution to this problem. Any suggestions are appreciated.
Parammust be the first command in your script to work correctly