 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to use the ValidateLength attribute in PowerShell?
The ValidateLength attribute in PowerShell is used to validate the length of the String. Generally how we write the command without the mentioned attribute is using the Length method and if/else condition for the string. For Example,
Function ValidateStorageName {    param (       [String]$StorageName    )    if(($StorageName.Length -gt 3) -and ($StorageName.Length -lt 15)) {       Write-Output "`nStorage Name validated"    } else {       Write-Output "`nStorage Name validation failed"    } } Output−
PS C:\> ValidateStorageName -StorageName Alpha Storage Name validated PS C:\> ValidateStorageName -StorageName CN Storage Name validation failed
With the ValidateLength attribute, else condition will work itself if the condition doesn’t satisfy.
Function ValidateStorageName {    param (       [ValidateLength(3,15)]       [String]$StorageName    )    Write-Output "Storage Name validated" }  Output−
PS C:\> ValidateStorageName -StorageName Alpha Storage Name validated PS C:\> ValidateStorageName -StorageName CN ValidateStorageName: Cannot validate argument on parameter 'StorageName'. The cha racter length (2) of the argument is too short. Specify an argument with a length that is greater than or equal to "3", and then try the command again
Advertisements
 