function foo
{
param(
[parameter(Mandatory=$true)]
[ValidateSet('Test1','Test2','Test3')]
[String]$ChooseOne= 'Test1',
)
do xxxxxxxxxxxxxxxx
{
foo -ChooseOne Test9
我正在使用一组必需参数来开发一个 powershell 函数。验证集效果很好,但是验证集是..如果输入的选项不是列出的选项之一..我会收到如下错误:
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
有没有办法让错误操作将错误的变量设置为之前定义的默认值?
所以在这种情况下,它将默认$ChooseOne
为“ Test1
”