ValidateLength Powershell

后端 未结 3 1485
渐次进展
渐次进展 2021-01-20 18:28

This is my first script so don\'t beat me up to bad!

I\'m working on a script that creates a network directory & AD group based on user input. The following is w

相关标签:
3条回答
  • 2021-01-20 18:48

    As @briantist already pointed out, validation functions were made for a different purpose (parameter validation). You have a different scenario, since you want to keep prompting the user until a valid value is entered. Validation functions just don't work well in this scenario for a number of reasons:

    • they throw an error instead of returning a boolean value, which is fine for a function call, but not for validating a value.
    • they don't repeat, so you need additional code anyway,
    • their parameters can't be variables.

    I would probably put the input/validation loop in a function and use numeric comparisons instead of checking the length of a string, since you're asking the user to input numbers anyway.

    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
    
    0 讨论(0)
  • 2021-01-20 18:51

    Although the various [Validate... attributes work on variables, it's a non-standard usage (and few people know about them). It works best when you do want to error out.

    If you don't, just check it yourself and decide and what to do in the event that it's not what you want:

    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)
    
    0 讨论(0)
  • 2021-01-20 19:01

    You could utilize a while loop:

    $Division = $null
    while ($null -eq $Division) {
        [ValidateLength(2,2)]$Division = [string](Read-Host -Prompt 'Please enter the TWO digit division number ')
    }
    
    0 讨论(0)
提交回复
热议问题