en-us/about_Scripting_ValidateLength.help.txt

TOPIC
    about_Scripting_ValidateLength
 
SHORT DESCRIPTION
    A tutorial on validating a parameter by its length.
     
LONG DESCRIPTION
    You can use this attribute in your PowerShell scripting to validate that
    a parameter value is at least a certain length and no more and a certain
    length. In other words, it has to be just right. Here’s what it looks
    like:
 
    [ValidateLength(2,10)]
    [string]$Fooby
     
    In this example any value for -Fooby must be at least 2 characters long
    and no more than 10. Anything outside of that range and PowerShell will
    raise an exception. You have to supply both values in the validation
    attribute. An alternative would be to use [ValidateScript()].
 
    [ValidateScript({ $_.Length -ge 5})]
    [string]$Name
     
    Now, any value for -Name must be at least 5 characters and there is no
    upper limit. Here’s a more complete example.
 
    #requires -version 2.0
    [cmdletbinding(SupportsShouldProcess=$True)]
 
    Param (
    [Parameter(Position=0,Mandatory=$True,
    HelpMessage="Enter name between 5 and 15 characters")]
    [ValidateLength(5,15)]
    [string]$Name,
    [Parameter(Position=1,Mandatory=$True,
    HelpMessage="Enter password between 7 and 64 characters")]
    [ValidateLength(7,64)]
    [ValidatePattern({^\S+$})]
    [string]$Password,
    [string]$Computername=$env:computername,
    [switch]$Passthru
    )
 
    Write-Host "Creating $name with password of $Password on $computername" `
    -ForegroundColor Green
     
    [ADSI]$Server="WinNT://$computername"
    $User=$server.Create("User",$Name)
 
    if ($pscmdlet.ShouldProcess($User.Path)) {
        Write-Host "Committing new account changes" -ForegroundColor Green
         
        <#
          #uncomment the next lines if you really, really want to do this
        $User.SetInfo()
        Write-Host "Setting password" -ForegroundColor Green
        $User.SetPassword($Password)
        If ($passthru) {
            Write-Output $User
        }
        #>
    }
     
    This script will create a local user account. I’m asking that the user
    name be between 5 and 15 characters and that the password be between 7
    and 64 characters. I’ve also added a second validation check on the
    password using [ValidatePattern()] to verify it doesn’t contain any
    spaces.
 
    The exception message you see with [ValidateLength()] depends on
    where you fall short.
 
    PS C:\Scripts> .\Demo-ValidateLength.ps1 Jeff Password123
    C:\scripts\Demo-ValidateLength.ps1 : Cannot validate argument on
    parameter 'Name'. The number of characters (4) in the argument is
    too small. Specify an argument whose length is greater than or equal
    to "5" and then try the command again
    .
    At line:1 char:26
    + .\Demo-ValidateLength.ps1 <<<< Jeff Password123
        + CategoryInfo : InvalidData: (:) [Demo-ValidateLength.ps1],
        ParameterBindingValidationException
        + FullyQualifiedErrorId : ParameterArgumentValidationError,Demo-
        ValidateLength.ps1
 
    PS C:\Scripts> .\Demo-ValidateLength.ps1 JeffJeffJeffJeff Password123
    C:\scripts\Demo-ValidateLength.ps1 : Cannot validate argument on
    parameter 'Name'. The argument length of 16 is too long. Shorten the
    length of the argument to less than or equal to "15" and then try the
    command again.
    At line:1 char:26
    + .\Demo-ValidateLength.ps1 <<<< JeffJeffJeffJeff Password123
        + CategoryInfo : InvalidData: (:) [Demo-ValidateLength.ps1],
        ParameterBindingValidationException
        + FullyQualifiedErrorId : ParameterArgumentValidationError,Demo-
        ValidateLength.ps1
     
    You can also use this attribute to verify that a value is of an exact
    length, by making the minimum and maximum values the same.
     
    [ValidateLength(5,5)]
    [string]$Name
     
    Now, any value for $Name must be exactly 5 characters long.
 
SEE ALSO
    about_Functions_Advanced_Parameters
    http://jdhitsolutions.com/blog