Map Dynamic Ext IP Address to Internal MS DNS Server

May 24, 2013 at 7:20 AM
#1st this script synchronises your external IP with your [afraid.org]
#dynamic DNS host (or #any compatible alternative).
#
#2nd it create and updates forward and reverse records on a local
#Microsoft DNS Server for any external domains you have pointing
#at your external IP address.
#
#You need to install DNSShell  http://dnsshell.codeplex.com/  and
#add it to your MS PowerShell plug-ins profile.  You can then launch
#the script manually, or create a job in the MS Task Scheduler.
#
#David Wilk  daw@g2online.co.uk - MapExtIP2IntDNS.ps1
#Script created 24th May 2013.  Version 1.0.0c
#
#You are free to modify the script code and distribute on condition
#and agreement not to remove or change any commented details.
#The comments are there to assist others, provide contact detail for
#query and feedback, acknowledge the author's time, effort and
#donation to the freeware community.
#
#Useful secondary whatismyip verifiers (all tested as of release date)...
#ifconfig.me/ip
#icanhazip.com
#myip.dnsomatic.com
#gbshouse.com/ip.php
 
#START
 
Param ($dynData, $idData, $ipData, $ptrData, $revData)
#sync external IP with dynamic DNS host
       $dynData = (Invoke-WebRequest "freedns.afraid.org/dynamic/update.php?YOUR-AFRAID.ORG-PERSONAL-REGISTRATION-STRING-HERE")
#cache external IP to variable
   If ($dynData -Match "(?<Address>((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))") {$dynData = $Matches.Address}
#create/update external IP forward [A] record
       $idData = (Get-dnsRecord -RecordType A -Server "YOURDNSSERVER" -ZoneName "YOURDOMAIN.WOT").Identity
   If ($idData -eq $Null) {New-dnsRecord -IPAddress $dynData -RecordType A -Server "YOURDNSSERVER" -ZoneName "YOURDOMAIN.WOT"}
       $ipData = (Get-dnsRecord -RecordType A -Server "YOURDNSSERVER" -ZoneName "YOURDOMAIN.WOT").RecordData
   If ($ipData -ne $dynData) {
           Set-dnsRecord -Address $dynData -Identity $idData
#cache external IP reverse [PTR] record
       $ptrData = (Get-dns $ipData -RecordType PTR -Server "YOURDNSSERVER").Question.Name.TrimEnd(".")
#delete stale external IP reverse [Zone] and [Records]
       $revData = $ptrData -Replace("^\d*.\d*.")
       $idData = (Get-dnsZone -Server "YOURDNSSERVER" -ZoneName $revData).Identity
       Remove-DnsObject -Identity $idData -Force
                             }
#cache external IP reverse [PTR] record
       $ptrData = (Get-dns $dynData -RecordType PTR -Server "YOURDNSSERVER").Question.Name.TrimEnd(".")
#validate/create external IP reverse [Zone]
       $revData = $ptrData -Replace("^\d*.\d*.")
       $idData = (Get-dnsZone -Server "YOURDNSSERVER" -ZoneName $revData).Identity
   If ($idData -eq $Null) {New-dnsZone -Server "YOURDNSSERVER" -ZoneName $revData -ZoneType Primary}
#create/update external IP reverse [PTR] record
       $idData = (Get-dnsRecord -Name $ptrData -RecordType PTR -ZoneName $revData -Server "YOURDNSSERVER").Identity
   If ($idData -eq $Null) {New-dnsRecord -HostName "YOURDOMAIN.WOT" -Name $dynData -RecordType PTR -Server "YOURDNSSERVER" -ZoneName $revData}
 
#END
 
Adieu, WolfWare.