是否可以使 Paramater 验证集与通配符一起工作?
我希望在 *
处接受 0-100。
param
(
[Parameter(Mandatory=$True)]
[validateset("6.1.*.*")]
[string]$variable
)
错误信息:
Cannot validate argument on parameter 'variable'. The argument "6.1.1.0" does not belong to the set "6.1.." specified by the ValidateSet attribute. Supply an argument that is in the set and then try the command again. + CategoryInfo : InvalidData: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : ParameterArgumentValidationError
最佳答案
因为看起来您要验证一个版本,您可能想要声明类型为[version]
的参数并使用ValidateScript
属性来验证值而不是使用字符串匹配:
function Test-Version {
param(
[ValidateScript({
$_.Major -eq '6' -and
$_.Minor -eq '1' -and
$_.Build -in (0..100) -and
$_.Revision -in (0..100) -or
$(throw 'Wrong Version')
})]
[version]$Version
)
}
关于powershell - 参数验证集通配符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47530279/