Hi Folks ,
If you are one of those lucky people that update DNS records for Windows 2008 then you might find this blog useful . Note:This blog is only for Windows 2008 DNS Server .There is not that much examples on the web for updating the DNS Server .
Since I do a lot of these updates I wanted to automate the process . I wanted to do this for 2 reasons . One its extremely fast for update 10 A and PTR records with scripting . Second reason is to cut down on errors . You can make errors easily when entering in a PTR record . So lets begin …..
Before we begin the script that I’m going to walk you thru Creates new A records and PTR records in DNS by reading a csv file . It doesn’t delete records but you can add that functionality easily .
We start by creating our Params . The way this script will work is by calling the script like this :
.\Update-DNS.ps1 -forward ServerName -reverse ServerName
This will become more apparent when I show the help file…
So my params line is first and it looks like this :
param ($forward,$reverse,$dnsaddresslist = (Import-Csv ".\dnsaddresslist.csv"),$help)
The great thing about powershell is that when you define your parameters first in your script ,you are able to use them like switches . So the $forward variable would be -forward SeverName .
The next section starts our functions starting with the help function :
function funHelp()
{
$helpText=@”
NAME: Update-DNSAddress.ps1
DESCRIPTION:
Creates DNS entries from a csv file called dnsaddresslist.csv .It
Creates a Forward and Reverse lookup Zone entry in the zone
servers specified.
Prerequisites:
You should have a dnsaddresslist.csv file in the same directory as the script.
When the script starts it reads this file. An error will occur if the file
is not present.
PARAMETERS:
-forward specifies the forward lookup zone Server (required)
-reverse specifies the reverse lookup sone Server (required)
-help Prints Help File
OTHER:
-dnsaddresslist Holds dns entry information in the csv file
SYNTAX:
.\Update-DNSAddress.ps1 -forward serverName -reverse ServerName
Creates forward and reverse entries from all devices listed
in dnsaddresslist.csv to the servers specified .
“@
$helpText
exit
}
I cannot take credit for the way this looks I have been reading the Microsoft powershell Book and Ed Wilson who is awesome creates these help functions which is great way to put help documention into a script .
Next I created a Check to see if the Forward and Reverse Servers are online .
function funCheck-DNSServersStatus ($forward,$Reverse)
{
Write-Host “Verifying if DNS Servers are Reachable…..”
# Create our object
$net = New-Object System.Net.NetworkInformation.Ping
#Check the Forward lookup Server
do {$result =$net.send($forward);}
until ($result.status -eq “Success”)
#Create message that Server is reachable
Write-Host “Forward lookup server ,$forward, is reachable …..”
#Check if the reverese Server is reachable .
do {$result =$net.send($reverse);}
until ($result.status -eq “Success”)
#Create message that the reverse lookup server is reachable
write-host “Reverse lookup server,$reverse,is reachable……”
}
We are passing the function both server name .If you check the function we are using the System.Net.NetworkInformation.Ping Class to communicate with the servers . Like a ping from a command prompt . Then using a Do … until loop .
Hopefully your DNS servers are up and running
. You can also add another check to see if the DNS service is running also which would be a good thing to check .
The next 2 functions are the update . You might be asking by I have separate functions for the forward and the reverse . Its becuase where I work we do A record updates on one server and PTR records on another Server . So the last 2 Functions look like this :
function funUpdate-forward($forward,$dnsaddresslist)
{
# Domain Name
$strDomain =”Test.Microsoft.com”
# create instance of ResourceRecord
$objRR = [WmiClass]“\\$forward\root\MicrosoftDNS:MicrosoftDNS_ResourceRecord”
foreach($a in $dnsaddresslist)
{
Write-Host ” Updating forward lookup zone with $a” -ForegroundColor RED
#create our ip address variable
$address = $a.Address
#create our A name record
$name = $a.Name + “Microsoft.com”
# create our String for record creation
$strRR = $name + ” IN A $address”
#Update Record now
$objRR.CreateInstanceFromTextRepresentation($forward,$strDomain,$strRR)
}
}
function funUpdate-Reverse($reverse,$dnsaddresslist)
{
# create instance of ResourceRecord
$objRR = [WmiClass]“\\$reverse\root\MicrosoftDNS:MicrosoftDNS_ResourceRecord”
foreach ($a in $dnsaddresslist)
{
Write-Host “Updating Reverse Lookup zone with $a” -ForegroundColor Blue
#create our ip address variable
$raddress = $a.Address
#Get the name record
$rname = $a.Name
#break the address into octets
$breakaddress = $raddress.split(‘.’)
#create octets
$rFirst = $breakaddress[0] ; $rSecond = $breakaddress[1] ;$rThird = $breakaddress[2] ; $rFourth = $breakaddress[3]
#create the Reverse lookup String
$strReverseRR = “$rFourth”+”.”+”$rThird”+”.”+”$rSecond”+” IN PTR $rname.test.microsoft.com”
$strReverseDomain = “$rFirst”+”.in-addr.arpa.”
#Call Create Method
$objRR.CreateInstanceFromTextRepresentation($reverse,$strReverseDomain,$strReverseRR)
}
}
Now lets look at the forward first :
We are using WMI to update both A and PTR records . So you need to create and instance .What we are doing is creating the IP address variable and Name variable . Then performing the Method Call which creates the record .
For the Reverse lookup it is much more difficult . We need to create an instance of our WMI object using the MicrosoftDNS_ResourceRecord class . We then read from the csv file and get both the IP and Name . Once we have those variables we reverse the IP address using this bit of code :
#break the address into octets
$breakaddress = $raddress.split(‘.’)
#create octets
$rFirst = $breakaddress[0] ; $rSecond = $breakaddress[1] ;$rThird = $breakaddress[2] ; $rFourth = $breakaddress[3]
#create the Reverse lookup String
$strReverseRR = “$rFourth”+”.”+”$rThird”+”.”+”$rSecond”+” IN PTR $rname.test.microsoft.com”
$strReverseDomain = “$rFirst”+”.in-addr.arpa.”
There is a lot going on here . We are breaking up the address then reversing it to create our string to pass to the method which we call to create our PTR record . Notice that in Windows 2008 you don’t have those cascading folders or Domain’s on the PTR zones like Windows 2003 . This script takes that into account so what ever address you are entering (10. or whatever ) it find the correct folder or Zone .
After going thru our Functions for this script it is time to get to the main portion .
#Check to see if help text is requested
if($help) { “Printing help now…”;funHelp}
#Check to see if forward and reverse arguments have been entered.
if(!$forward) {“You must Supply a forward lookup zone DNS server” ; funHelp}
if(!$reverse) {“You must Supply a reverse lookup zone DNS server” ; funhelp}
# Show the contents of the txt file and ask the user if they would like to continue
Write-Host “The following IP address/hosts will be entered in DNS.”
#contents file
$dnsaddresslist
#let the user make a descion if they would like to continue.
$decision = Read-Host “Would you like to continue Y or N–”
switch($decision.toupper())
{
Y{continue}
N{exit}
}
#Call to verify DNS Servers .
funCheck-DNSServersStatus $forward $reverse
#now that we have all the information lets update forward zone
funUpdate-forward $forward $dnsaddresslist
#update reverse zone
funUpdate-Reverse $reverse $dnsaddresslist
First we are seeing if help parameter has been passed if so it prints the Help file . Next it checks to make sure the Forward and Reverse Servers are passed which are required .
Next it prints out the csv file and gives the user a decsion to continue or not . Sort of like a warning .After all we are updating some important stuff .
Last but not least we call the functions .
Here is the finished Script :
####################################################################
#ScriptName : update-DNSAddress
#Created by : Chris Federico
#Date Created : 09/09/2008
#Modifications:
###################################################################param ($forward,$reverse,$dnsaddresslist = (Import-Csv ".\dnsaddresslist.csv"),$help)
function funHelp()
{
$helpText=@”
NAME: Update-DNSAddress.ps1
DESCRIPTION:
Creates DNS entries from a csv file called dnsaddresslist.csv .It
Creates a Forward and Reverse lookup Zone entry in the zone
servers specified.
Prerequisites:
You should have a dnsaddresslist.csv file in the same directory as the script.
When the script starts it reads this file. An error will occur if the file
is not present.
PARAMETERS:
-forward specifies the forward lookup zone Server (required)
-reverse specifies the reverse lookup sone Server (required)
-help Prints Help File
OTHER:
-dnsaddresslist Holds dns entry information in the csv file
SYNTAX:
.\Update-DNSAddress.ps1 -forward ServerName -reverse ServerName
Creates forward and reverse entries from all devices listed
in dnsaddresslist.csv to the servers specified .
“@
$helpText
exit
}
function funCheck-DNSServersStatus ($forward,$Reverse)
{
Write-Host “Verifying if DNS Servers are Reachable…..”
# Create our object
$net = New-Object System.Net.NetworkInformation.Ping
#Check the Forward lookup Server
do {$result =$net.send($forward);}
until ($result.status -eq “Success”)
#Create message that Server is reachable
Write-Host “Forward lookup server ,$forward, is reachable …..”
#Check if the reverese Server is reachable .
do {$result =$net.send($reverse);}
until ($result.status -eq “Success”)
#Create message that the reverse lookup server is reachable
write-host “Reverse lookup server,$reverse,is reachable……”
}
function funUpdate-forward($forward,$dnsaddresslist)
{
# Domain Name
$strDomain =”Microsoft.com”
# create instance of ResourceRecord
$objRR = [WmiClass]“\\$forward\root\MicrosoftDNS:MicrosoftDNS_ResourceRecord”
# We have to read in the txt file split it to get IP address and Name
foreach($a in $dnsaddresslist)
{
Write-Host ” Updating forward lookup zone with $a” -ForegroundColor RED
#create our ip address variable
$address = $a.Address
#create our A name record
$name = $a.Name + “FQDNS_NAME”
# create our String for record creation
$strRR = $name + ” IN A $address”
#Update Record now
$objRR.CreateInstanceFromTextRepresentation($forward,$strDomain,$strRR)
}
}
function funUpdate-Reverse($reverse,$dnsaddresslist)
{
# create instance of ResourceRecord
$objRR = [WmiClass]“\\$reverse\root\MicrosoftDNS:MicrosoftDNS_ResourceRecord”
foreach ($a in $dnsaddresslist)
{
Write-Host “Updating Reverse Lookup zone with $a” -ForegroundColor Blue
#create our ip address variable
$raddress = $a.Address
#Get the name record
$rname = $a.Name
#break the address into octets
$breakaddress = $raddress.split(‘.’)
#create octets
$rFirst = $breakaddress[0] ; $rSecond = $breakaddress[1] ;$rThird = $breakaddress[2] ; $rFourth = $breakaddress[3]
#create the Reverse lookup String
$strReverseRR = “$rFourth”+”.”+”$rThird”+”.”+”$rSecond”+” IN PTR $rname.microsoft.com
$strReverseDomain = “$rFirst”+”.in-addr.arpa.”
#Call Create Method
$objRR.CreateInstanceFromTextRepresentation($reverse,$strReverseDomain,$strReverseRR)
}
}
#Check to see if help text is requested
if($help) { “Printing help now…”;funHelp}
#Check to see if forward and reverse arguments have been entered.
if(!$forward) {“You must Supply a forward lookup zone DNS server” ; funHelp}
if(!$reverse) {“You must Supply a reverse lookup zone DNS server” ; funhelp}
# Show the contents of the txt file and ask the user if they would like to continue
Write-Host “The following IP address/hosts will be entered in DNS.”
#contents file
$dnsaddresslist
#let the user make a descion if they would like to continue.
$decision = Read-Host “Would you like to continue Y or N–”
switch($decision.toupper())
{
Y{continue}
N{exit}
}
#Call to verify DNS Servers .
funCheck-DNSServersStatus $forward $reverse
#now that we have all the information lets update forward zone
funUpdate-forward $forward $dnsaddresslist
#update reverse zone
funUpdate-Reverse $reverse $dnsaddresslist
**** Update … I forgot to mention the csv file should look like this :
Name,Address
