powershell - 参数验证集通配符

标签 powershell

是否可以使 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/

相关文章:

powershell - 如何删除文件夹中除只读文件外的所有文件?

regex - PowerShell Try 语句缺少其 Catch 或 finally block

json - 请求中的参数 NO_PARAM 无效请在使用 Powershell 部署 azure arm 模板时为参数 NO_PARAM 提供正确的值

sharepoint - PowerShell-具有UniquePermissions的新SPWeb

c# - 获取 PowerShell 脚本以在 Azure 上运行 SharePoint 命令

c# - 使用 PowerShell 和模块化 GUI 吗?

powershell - 将最新的窗口更新导出到Powershell中的CSV

azure - 使用 ARM 或 powershell 设置 Azure SQL Server 防火墙关闭

powershell - 如何将多个值从 powershell 脚本返回到调用它的批处理文件?

azure - 无法将类型 "System.String"的值转换为类型 "System.Collections.Hashtable"