Created
May 21, 2020 20:12
-
-
Save tommymaynard/34963faf0d659e2c28f5ef08f6ca840b to your computer and use it in GitHub Desktop.
Check Email Address Domain Against the Top-Level Domain List from IANA
This file contains 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
<# | |
TechNet Contribution: Check Email Address Domain Against the Top-Level Domain List from IANA | |
Previous link: https://gallery.technet.microsoft.com/Check-Email-Address-Domain-41cb7838 | |
Downloaded: 496 times (as of 05/21/2020) | |
At times it seems that there has been some frustration when trying to validate an email address. In my experience, | |
this often been because an email address without a domain (.com, .net, .org) is often validated as True. While the | |
purpose of this advanced function is not to validate an entire email address, it does validate the domain according | |
to the Internet Assigned Numbers Authority (IANA). | |
This advanced function will download a top-level domain (TLD) list from IANA, store it in a file, and then compare | |
he domain of an email address, or a given string, against the TLD file. It has other features that will allow it work | |
from an existing TLD file, if the file has already been downloaded in the last day, or if there is no Internet | |
connection. By default, it will only return True or False, but using the -MoreInfo and -ShowVars parameters will return | |
more information. In addition, there is a -Days parameter that can be useful as well. Using this parameter will force | |
updating the TLD file (-Days -1) or force the function to use the TLD file, providing it already has one, even if it is | |
over a day old (-Days 20, -Days 100, etc.). | |
Finally, this advanced function only works with Windows PowerShell 3.0 and greater | |
Please report any problems that you may find so they can be corrected. | |
Tommy Maynard | |
http://tommymaynard.com | |
#> | |
Function Find-TMEmailTLD { | |
##### ** THIS SCRIPT IS PROVIDED WITHOUT WARRANTY, USE AT YOUR OWN RISK ** | |
<# | |
.SYNOPSIS | |
Determines if a string is on the top-level domain (TLD) list from Internet Assigned Numbers Authority (IANA) (https://www.iana.org/). | |
.DESCRIPTION | |
The Find-TMEmailTLD advanced function accepts a string as input. If the string includes a dot character, such as expected in an email address, it will check the string after the final dot character. If it does not include a dot character it will simply check the provided string. It will ultimately compare the the string against the TLD file that is created by downloading a list of TLDs provided by IANA. | |
.PARAMETER String | |
This is the default, required parameter. It is the string that the function will compare to the TLD file. | |
.PARAMETER Days | |
By default the script will not update the TLD file, providing it has already has a TLD file, unless the file was last written over 1 day ago. This parameter allows the user to change the default value to as many or as few days as desired. Use a negative number to force the TLD file download or a very large number if the file has already been downloaded and a new one is not needed, or there is no Internet connection. | |
.PARAMETER MoreInfo | |
By default the function will only return True or False. Using this parameter will include a small amount of additional information. | |
.PARAMETER ShowVars | |
Using this parameter will tell the function to display the values stored in the variables used by this function. Although this is a carryover from development, it could be helpful in both understanding the function and troubleshooting any errors. | |
.EXAMPLE | |
Find-TMEmailTLD -String user@domain.com | |
This example will isolate com and check it against the TLD file from IANA. By default this function only returns True or False. | |
.EXAMPLE | |
Find-TMEmailTLD mil -MoreInfo -ShowVars | |
The example will check mil against the TLD file from IANA. In addition, it will display a few other pieces of information, including the values stored in the variables used in the script. | |
.EXAMPLE | |
Find-TMEmailTLD xxy -Days 10 | |
This example will check xxy against the TLD file from IANA. In addition, it will only download a new version of the TLD file if the file already exists and is older than 10 days. | |
.NOTES | |
NAME: Find-TMEmailTLD | |
AUTHOR: Tommy Maynard | |
LASTEDIT: 06/16/2014 | |
VERSION: 1.2 | |
NOTES: This is the first version (1.2) uploaded to the TechNet Gallery. | |
#> | |
[CmdletBinding()] | |
Param( | |
[Parameter(Mandatory=$True,Position=0)] | |
[string]$String, | |
[Parameter(Position=1)] | |
[int]$Days = 1, | |
[switch]$MoreInfo, | |
[switch]$ShowVars | |
) | |
Begin { | |
##### Test for Internet Connection ; requires ICMP/Ping to Internet | |
Write-Verbose -Message 'Checking for Internet connection' | |
If ((Test-Connection 'pingomatic.com' -Quiet -Count 1) -or (Test-Connection -ComputerName 'pinging.com' -Quiet -Count 1)) { | |
Write-Verbose -Message 'Internet connecion available' | |
$InternetConnection = $True | |
} Else { | |
$InternetConnection = $False | |
} | |
##### Checking for TLD File ; Downloading if necessary | |
Write-Verbose -Message 'Checking for TLD file from IANA' | |
If ($PSVersionTable.PSVersion.Major -le 2) { | |
Write-Warning -Message 'Run this function with the -MoreInfo parameter.' | |
$WrongVersion = $True | |
$OutputCode = 6 | |
} ElseIf ($PSVersionTable.PSVersion.Major -ge 3) { | |
$FileLocation = "$PSScriptRoot\tlds.txt" | |
} | |
If ($WrongVersion -ne $True) { | |
$FileLocation = "$PSScriptRoot\tlds.txt" | |
If (Test-Path -Path $FileLocation) { | |
Write-Verbose -Message 'TLD file from IANA found' | |
$FileInfo = Get-ChildItem -File $FileLocation | select -ExpandProperty LastWriteTime | |
$TimeSinceDownload = New-TimeSpan -Start $FileInfo -End (Get-Date) | Select-Object -ExpandProperty Days | |
Write-Verbose -Message 'Checking TLD file age' | |
If ($TimeSinceDownload -ge $Days -and $InternetConnection -eq $True) { | |
Write-Verbose -Message "TLD file needs to be replaced (Days: $Days)" | |
$GetNewFile = $True | |
$LoadExistingFile = $False | |
} | |
Else { | |
Write-Verbose -Message 'TLD file does not need to be replaced' | |
$LoadExistingFile = $True | |
$GetNewFile = $False | |
} | |
} ElseIf ($InternetConnection -eq $True) { | |
Write-Verbose -Message 'TLD file needs to be replaced' | |
$GetNewFile = $True | |
} Else { | |
$TLDList = $NULL | |
} | |
##### Get New File or Load Existing TLD File | |
If ($GetNewFile) { | |
try { | |
Write-Verbose -Message 'Downloading TLD file' | |
$FileURL = 'http://data.iana.org/TLD/tlds-alpha-by-domain.txt' | |
$TLDList = (Invoke-WebRequest -Uri $FileURL).Content | |
Set-Content -Path $FileLocation -Value $TLDList | |
$TLDList = Get-Content -Path $FileLocation | Where-Object {$_ -notlike '#*' -and $_ -notlike ''} | |
} | |
catch { | |
$TLDList = $NULL | |
Write-Warning -Message 'Run this function with the -MoreInfo parameter.' | |
} | |
} ElseIf ($LoadExistingFile) { | |
Write-Verbose -Message 'Loading existing TLD file' | |
$TLDList = Get-Content -Path $FileLocation | Where-Object {$_ -notlike '#*' -and $_ -notlike ''} | |
} Else { | |
$TLDList = $NULL | |
} | |
} | |
} # End Begin | |
Process { | |
##### Check string against TLD list | |
If ($WrongVersion -ne $True) { | |
Write-Verbose -Message 'Checking that TLD file has entries/values' | |
If ($TLDList -ne $NULL) { | |
Write-Verbose -Message 'Checking string against TLD file' | |
$String = ($String.Split('.')[-1]).Trim().ToLower() | |
If (-not($String.Length -le 0)) { | |
If ($TLDList | Select-String "^$String$") { | |
Write-Output -Verbose $True | |
$OutputCode = 1 | |
} ElseIf (-not($TLDList | Select-String "^$String$")) { | |
Write-Output -Verbose $False | |
$OutputCode = 2 | |
} | |
} Else { | |
$OutputCode = 3 | |
Write-Output -Verbose $False | |
} | |
} ElseIf ($InternetConnection -eq $False -and $TLDList -eq $NULL) { | |
$OutputCode = 4 | |
} ElseIf (($InternetConnection -eq $True -and $TLDList -eq $NULL) -or ($TLDList -eq $NULL)) { | |
$OutputCode = 5 | |
} Else { | |
$OutputCode = 7 | |
} | |
} | |
##### Write output message (-MoreInfo parameter) | |
If ($MoreInfo) { | |
Write-Verbose -Message 'MoreInfo parameter specified' | |
switch ($OutputCode) { | |
1 {Write-Output -Verbose "SUCCESS: $String`r`nMESSAGE: This TLD matches the TLD file from IANA."} | |
2 {Write-Output -Verbose "FAILURE: $String`r`nMESSAGE: This TLD does not match the TLD file from IANA."} | |
3 {Write-Output -Verbose "ERROR: $String`r`nMESSAGE: A usable string has not been entered."} | |
4 {Write-Output -Verbose "ERROR: $String`r`nMESSAGE: No Internet Connection and No TLD file."} | |
5 {Write-Output -Verbose "ERROR: $String`r`nMESSAGE: IANA URL may have changed ($FileURL) or no access to write TLD file (Check UAC)."} | |
6 {Write-Output -Verbose "ERROR: Version`r`nMESSAGE: This advanced function requires Windows PowerShell 3.0 or greater."} | |
7 {Write-Output -Verbose "ERROR: $String`r`nMESSAGE: Unknown Error (Output Code: $OutputCode"} | |
default {Write-Output -Verbose "ERROR: $String`r`nMESSAGE: Unknown error (Output Code: Default)."} | |
} | |
} | |
##### Display variables' values (-ShowVars parameter) | |
If ($ShowVars) { | |
Write-Verbose -Message 'ShowVars parameter specified' | |
Write-Output "----- Variables -----" | |
"String: $String", | |
"Internet Connection: $InternetConnection", | |
"File Location: $FileLocation", | |
"File Last Write Time: $FileInfo", | |
"Days Since Download: $TimeSinceDownload day(s)" | |
"Days Specified: $Days day(s)", | |
"Get New File: $GetNewFile", | |
"Load Existing File: $LoadExistingFile", | |
"Output Code: $OutputCode" | |
"Show Variables: $ShowVars" | |
} | |
} # End Process | |
} # End Function |
Author
tommymaynard
commented
May 21, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment