function foo
{
param(
[parameter(Mandatory=$true)]
[ValidateSet('Test1','Test2','Test3')]
[String]$ChooseOne= 'Test1',
)
do xxxxxxxxxxxxxxxx
{
foo -ChooseOne Test9
I'm working on a powershell function with a set of required parameters. The validate set works great, however as Validate Set is.. if an option is entered that is not one of the listed options.. I'll get an error like below:
foo: Cannot validate argument on parameter 'ChooseOne'. The argument "Test9" does not belong to the set "Test1,Test2,Test3" specified by the ValidateSet attribute. Supply an argument that is in the set and then try the
command again.
At line:1 char:1
+ foo
+ ~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [foo], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,foo
Is there a way to have an error action to set the wronged variable to it's default that was previously defined?
So in this case it would default $ChooseOne
to "Test1
"