Powershell Script to add ‘A’ Record “PTR’ Record in DNS

Posted on: November 16, 2012

I was participating in Powershell TechNet forum and someone asked question about how to add the ‘A’ Records and ‘PTR’ Records in DNS server . I thought it will be a good excercise for me , ( As I still have sleepless night when I think of Powershell :))

After working for long hours , I have built below script.

Note- One should have C:\DNS.CSV with columns as Hostname and IP address which needs to assinged. 

PowerShell Script

#Script to add DNS ‘A’ Records ‘PTR’ Records to DNS Servers
$dns = “DC” # Your DNS Server Name Goes here
$Zone = “AD.COM” # Your Forward Lookup Zone Name Goes here
$ReverseZone = “” # Your ReverseLookup Zone Name Goes Here
$a = import-csv C:\DNS.csv

#Preparing the C:\Reverse.csv from C:\DNS.CSV for Adding PTR Records
$b = $a | Select-Object -expand IP
$c = $b | %{$_.Split(“.”) | Select-Object -Index 3}
$d = $a | Select-Object -Expand Name
$e = $d | %{$_.Insert($_.length,””)}
for($i=0;$i -le ($e.Length);$i++)
(‘”{0}”,”{1}”‘ -f $c[$i],$e[$i]) | Out-File C:\Reverse.csv -Append -Encoding ascii

$header = “IP”,”Name”
$f = Import-Csv C:\Reverse.csv  -Header $header

#Adding ‘A’ Record to DNS Forward Lookup Zone
$a | %{dnscmd $dns /recordadd $Zone $($_.Name)A $($_.IP)}

#Adding ‘PTR’ Record to DNS Reverse Lookup Zone
$f | %{dnscmd $dns /recordadd $ReverseZone $($_.IP)PTR $($_.Name)}

Script will produce one more CSV File C:\Reverse.CSV which contains IP and FQDN Name Columns for PTR Record creation

Hope this script help AD Administrators 🙂






2 Responses to "Powershell Script to add ‘A’ Record “PTR’ Record in DNS"

Thanks for your personal marvelous posting! I certainly enjoyed reading it, you may
be a great author. I will ensure that I bookmark your blog and may come back in the future.
I want to encourage one to continue your great work, have a nice holiday weekend!

Spot on with this write-up, I truly feel this amazing site needs
much more attention. I’ll probably be returning to read more, thanks for the information!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: