I wondered that scripts whose parameter are validated by the ValidateSet
attribute do not return any invalid $LASTEXITCODE
when invalid parameters are given.
Let's say I have the following PowerShell script, called try.ps1
:
#INPUT Variables for the Script
Param(
[Parameter(Mandatory)]
[ValidateSet("a", "b")]
[string] $first_param,
[Parameter(Mandatory)]
[ValidateSet("c", "d")]
[string] $second_param
)
exit 0
If I call it now in PS via PS C:\Users\MyUser> .\try.ps1 -first_param n -second_param c
(invalid $first_param
) I get a ParameterBindingValidationException
with an Error called ParameterArgumentValidationError
but this seems not not set my $LASTEXITCODE
variable, but why not?
So how is it possible for me (without bloating my code and validating each of my variables in some if-else
statements to trigger an error) to get $LASTEXITCODE=1
back when calling my script with invalid params?