Use the Test-Connection cmdlet to determine when a DNS record has propagated

For some time now I’ve wanted some sort of service that would simply alert me via Email once a specific DNS record had propagated and was reachable by at least on host on the public internet. To my knowledge, such a service doesn’t exist. I was faced with a situation this morning where such a solution would have been extremely helpful. So, I decided to sit down and break out PowerGUI Script Editor to make my dream a reality! What emerged from 20 minutes of hackery was a 13 line script that works like this:

  1. a user inputs a few variables so the script knows what to ping, where to send an Email notification, etc.

  2. The script executes a simple function that uses the Test-Connection cmdlet to attempt to resolve a hostname. If successful, you get an Email informing you of the success. If not, the script will simply exit.

A couple bits of housekeeping:

  1. You need an SMTP server that you have permission to either send or relay through. On-premises or otherwise.

  2. Please provide fully qualified Email addresses such as alias@domain.com

Simply schedule this script to run at a specified interval using Task Scheduler then go on about your day and let PowerShell look for your DNS record.

Finally, here’s an example of the Email you will receive when the specified DNS record has propagated.

 

 

 

 

notify-ondnsprop.ps1

[ps]# Author: Josh Bolling
# Date: 6/3/2011
# http://www.joshbolling.com
$dest = Read-Host "Enter the name of the DNS record you would like to query for"
$smtpserver = Read-Host "Enter the FQDN or IP address of your SMTP server"
$from = Read-Host "Email addrress to send notification from"
$to = Read-Host "Email address to send notification to"
function notify-onprop{if ((Test-Connection $dest -Quiet) -eq $true)
{Send-MailMessage -SMTPServer $smtpserver -from $from -To $to -Subject &quot;DNS record is now reachable&quot; -Body &quot;Hello,</p>
<p>The DNS record you were querying for is now reachable.&quot; -BodyAsHTML | Out-Null}
else {}
}
notify-onprop[/ps]

Download
Download notfiy-ondnsprop.ps1

Note: I’m sure much more elegant ways of querying for a DNS records with PowerShell exist. This method was hacked up in around 20 minutes to satisfy a rather immediate need.

Leave a Reply

Your email address will not be published. Required fields are marked *