Input values are validated using various mechanisms. let’s start by examining how you would validate input parameters in Windows PowerShell 2.0
The Inputs are validated for the below mentioned functionality
- PostalCode
- PhoneNumber
- EmailID
- IP
- FileName
Download the scripts from the below link
http://gallery.technet.microsoft.com/PowerShell-Input-Validation-30ebaa51
To validate PostalCode – 99999
Function Validate-PostalCode
{
Param([Parameter(Mandatory=$true,HelpMessage="Enter a valid Postal Code xxx-xxx-xxxx")][ValidatePattern("[0-9][0-9][0-9][0-9]")]$PostalCode)
Write-host "The Pin Code $PostalCode is valid"
}
To Validate phone number – 999-999-9999
Function validate-PhoneNumber
{
Param([ValidatePattern("\d{3}-\d{3}-\d{4}")]$phoneNumber)
Write-host "The phone number $phoneNumber is valid"
}
validate-PhoneNumber -Phonenumber 999-999-9999
To Validate email address
function Validate-Email ([string]$Email)
{
return $Email -match "^(?("")("".+?""@)|(([0-9a-zA-Z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-zA-Z])@))(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,6}))$"
}
To Validate IP Range – 255.255.255.255
function Validate-IPAddress ([string]$IP)
{
if($IP -match "(\d{1,3}).(\d{1,3}).(\d{1,3}).(\d{1,3})" -and -not ([int[]]$matches[1..4] -gt 255))
{
Write-host "The $IP IP is valid"
}
}
To Validate the filename for ########_???_?.jpg
function Validate-Filename
{
Param([ValidatePattern("^\d{8}_[a-zA-Z]{3,4}_[a-zA-Z]{1}\.jpg"]$filename)
Write-host "The filename $filename is valid"
}
You can also refer the below link for more information
About Prashanth Jayaram
DB Technologist, Author, Blogger, Service Delivery Manager at CTS, Automation Expert, Technet WIKI Ninja, MVB and Powershell Geek
My Profile:
https://social.technet.microsoft.com/profile/prashanth jayaram/
http://www.sqlshack.com/author/prashanth/
http://codingsight.com/author/prashanthjayaram/
https://www.red-gate.com/simple-talk/author/prashanthjayaram/
http://www.sqlservercentral.com/blogs/powersql-by-prashanth-jayaram/
Connect Me:
Twitter @prashantjayaram
GMAIL
[email protected]
The articles are published in:
http://www.ssas-info.com/analysis-services-articles/
http://db-pub.com/
http://www.sswug.org/sswugresearch/community/