【问题标题】:ValidateLength Powershell验证长度 Powershell
【发布时间】:2017-02-25 05:37:18
【问题描述】:

这是我的第一个脚本,所以不要打我!

我正在编写一个基于用户输入创建网络目录和 AD 组的脚本。以下是我到目前为止所得到的。它可以工作,但我想进行一些改进。

我想验证用户输入的长度。我找到了一篇文章 (PowerShell ValidateLength with Read-Host),它解释了如何使用 ValidateLength 字符串来检查用户的输入。这很好用,但是,我想将它包含在一个循环中。例如,如果用户没有准确输入 X 个字符 - 然后重试。现在它只是出错了。

任何帮助将不胜感激!

[ValidateLength(2,2)]$Division = [string](Read-Host -Prompt 'Please enter the TWO digit division number ')
[ValidateLength(4,4)]$Matter = [string](Read-Host -Prompt 'Please enter the FOUR digit matter number ')
[ValidateLength(4,4)]$Client = [string](Read-Host -Prompt 'Please enter the FOUR digit client number ')

【问题讨论】:

  • 如果您希望用户输入数字,为什么要验证转换为字符串的值的长度而不是转换为 int 的值的 validating the range

标签: powershell


【解决方案1】:

你可以利用一个while循环:

$Division = $null
while ($null -eq $Division) {
    [ValidateLength(2,2)]$Division = [string](Read-Host -Prompt 'Please enter the TWO digit division number ')
}

【讨论】:

    【解决方案2】:

    尽管各种[Validate... 属性适用于变量,但这是一种非标准用法(很少有人知道它们)。当您确实想要出错时,它最有效。

    如果您不这样做,请自行检查并决定如果不是您想要的,该怎么做:

    do {
        $Division = [string](Read-Host -Prompt 'Please enter the TWO digit division number ')
        if ($Divison.Length -ne 2) {
            continue
        }
    
        $Matter = [string](Read-Host -Prompt 'Please enter the FOUR digit matter number ')
        if ($Matter.Length -ne 4) {
            continue
        }
    
        $Client = [string](Read-Host -Prompt 'Please enter the FOUR digit client number ')
        if ($Client.Length -ne 4) {
            continue
        }
    
        break
    } while ($true)
    

    【讨论】:

    • @AnsgarWiechers 这是单个提示的示例,但他有 3 个提示,所以实际上我更新了它,但还有更清洁的方法。发布另一个答案;)
    • 谢谢!这很好用。我开始学习如何编写更强大的 do/whiles。
    • @JaredZ 太好了!很高兴它有帮助,但我强烈建议您使用(并接受)Ansgar 的回答。它更完整,更正确,如果我在会议之间不写一个关于 SO 的快速答案,我会这样做:)
    【解决方案3】:

    正如@briantist 已经指出的那样,验证函数是为不同的目的(参数验证)而设计的。你有一个不同的场景,因为你想一直提示用户直到输入一个有效的值。由于多种原因,验证函数在这种情况下无法正常工作:

    • 它们会抛出错误而不是返回布尔值,这对于函数调用很好,但不适用于验证值。
    • 它们不会重复,所以无论如何您都需要额外的代码,
    • 它们的参数不能是变量。

    我可能会将输入/验证循环放在一个函数中并使用数字比较而不是检查字符串的长度,因为无论如何您都要求用户输入数字。

    function Get-Number {
      [CmdletBinding()]
      Param(
        [Parameter(Mandatory=$true)]
        [string]$Prompt,
        [Parameter(Mandatory=$false)]
        [int]$Min = 0,
        [Parameter(Mandatory=$false)]
        [int]$Max = 9999
      )
    
      # prevent infinite loop due to impossible condition
      if ($Min -gt $Max) { throw 'Min cannot be greater than max.' }
    
      $ErrorActionPreference = 'SilentlyContinue'
      do {
        $num = Read-Host -Prompt $Prompt
        $valid = $Min -le $num -and $Max -ge $num
        if (-not $valid) { Write-Host "Invalid value: $num" }
      } until ($valid)
    
      return [int]$num
    }
    
    $Division = Get-Number -Prompt 'Please enter the TWO digit division number' -Min 10 -Max 99
    $Matter   = Get-Number -Prompt 'Please enter the FOUR digit matter number' -Min 1000 -Max 9999
    $Client   = Get-Number -Prompt 'Please enter the FOUR digit client number' -Min 1000 -Max 9999
    

    【讨论】:

    • 这绝对是一个更好的方法。为了检查有效性,我还喜欢使用范围:$valid = $Min..$Max -contains $num$valid = $num -in $Min..$Max
    • 谢谢!这帮助很大。
    猜你喜欢
    • 2018-03-07
    • 2022-12-24
    • 2021-08-09
    • 2020-12-11
    • 1970-01-01
    • 2016-02-19
    • 2022-01-27
    • 1970-01-01
    相关资源
    最近更新 更多