Created
October 14, 2015 21:44
-
-
Save torgro/98540b769a407d8c41de to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function Assert-EmailAddress | |
| { | |
| <# | |
| .Synopsis | |
| Validate Email address | |
| .DESCRIPTION | |
| Use this cmdlet to validate Email addresses. | |
| .EXAMPLE | |
| Assert-EmailAddress -StringEmail "test@domain.com" | |
| Outputs True | |
| .EXAMPLE | |
| "tore.somedomain.dom" | Assert-EmailAddress | |
| Outputs False | |
| .EXAMPLE | |
| $object = "" | Select-Object -Property Mail | |
| $object.Mail = "test@domain.com" | |
| $object | Assert-EmailAddress | |
| Outputs True | |
| .INPUTS | |
| [string] | |
| .OUTPUTS | |
| It outpus a boolean value indicating if it is a valid Email Address | |
| .NOTES | |
| Use at your own Risk! | |
| .COMPONENT | |
| General scripting | |
| .ROLE | |
| Mail space | |
| .FUNCTIONALITY | |
| Validating Email Addresses | |
| #> | |
| [OutputType([bool])] | |
| [cmdletbinding()] | |
| Param( | |
| [Parameter( | |
| Mandatory=$true, | |
| ValueFromPipeline=$true, | |
| ValueFromPipelineByPropertyName=$true, | |
| ValueFromRemainingArguments=$false | |
| )] | |
| [ValidateNotNullOrEmpty()] | |
| [Alias("Email","Mail","Address")] | |
| [string]$StringEmail | |
| ) | |
| Begin | |
| { | |
| $f = $MyInvocation.InvocationName | |
| Write-Verbose -Message "$f - START" | |
| } | |
| Process | |
| { | |
| Write-Verbose -Message "$f - Asserting email $StringEmail" | |
| try | |
| { | |
| $Email = New-Object -TypeName System.Net.Mail.MailAddress -ArgumentList $StringEmail | |
| if($Email.Address -eq $StringEmail) | |
| { | |
| Write-Verbose -Message "$f - Valid email address $StringEmail" | |
| return $true | |
| } | |
| else | |
| { | |
| Write-Warning -Message "$f - Invalid email address $StringEmail, was parsed to $($Email.Address)" | |
| return $false | |
| } | |
| } | |
| catch | |
| { | |
| Write-Warning -Message "$f - Invalid email address $StringEmail" | |
| return $false | |
| } | |
| } | |
| End | |
| { | |
| Write-Verbose -Message "$f - END" | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment