【发布时间】:2016-12-20 23:58:15
【问题描述】:
创建验证集很容易。
param(
[Parameter(Mandatory=$true)]
[ValidateSet('Ding','Dong')]
[string]$bellState,
[Parameter(Mandatory=$true)]
[ValidateSet('Dead','Alive')]
[string]$witchesState
)
如果您的 Powershell 版本 >2,它会提供免费的自动完成功能
但是,如果你一开始不传递参数,它就没有那么有用了。
cmdlet Untitled2.ps1 at command pipeline position 1
Supply values for the following parameters:
bellState: Dib
witchesState: Alive
C:\Users\cac\Untitled2.ps1 : Cannot validate argument on parameter 'bellState'. The argument "Dib" does not belong to the set "Ding,Dong" specified by the ValidateSet attribute. Supply an argument that is in the set and then
try the command again.
+ CategoryInfo : InvalidData: (:) [Untitled2.ps1], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Untitled2.ps1
这次没有tab补全或线索:(
如果你输入了无效的内容,你会得到一个有用的错误:
"The argument "Dib" does not belong to the set "Ding,Dong""
但是,这个错误是在参数末尾而不是在最初的错误发生时抛出的,并且没有重试的选项。
有没有人找到一种方法来扩展此验证,使其在未传入参数的情况下启动的实例更加用户友好。
【问题讨论】:
-
我不知道使用 ValidateSet 扩展的方法。我唯一知道的方法是使用
Read-Hostcmdlet 并自己动手,但我不建议您这样做。你可能应该忍受它。
标签: validation powershell user-interface