Bulk updating A records in DNS on Windows Server 2003
When in a Windows server hosting environment there may come a time when you have to make DNS updates for a whole bunch of A records. If you have a few, then the DNS MMC works fine, but if you have say 300 records that you need to change, it gets you thinking there has got to be a better way. There is, with a tool that comes installed on Windows 2003 Server called dnscmd.exe. (You can download dnscmd.exe in the Support Tools package for Windows XP: http://technet.microsoft.com/en-us/library/cc756116.aspx)
dnscmd.exe is a command-line interface for DNS management. With it you can among other things: RecordAdd and RecordDelete. Sounds good, now we just have to write a Batch file to loop over all the zones and make our desired updates.
*** Note: I am not a DNS expert nor a windows scripting expert, but so the following is just one way to solve this above stated problem, there may be other ‘better’ ways, but this one succeeded in accomplishing our goal ***
With dnscmd.exe we EnumZones into a text file we’ll call ‘zones.txt’ using the following command:
dnscmd dns.yourdomain.net /enumzones > zones.txt
This will result in a list of all zones you have setup (edit the file to strip out the top header and bottom footer).
Now to loop over all of these we’ll create a Batch file called ‘updateRecords.bat’ with the following code:
for /F "tokens=1-4 delims= " %%a in (zones.txt) do ( dnscmd dns.yourdomain.net /enumrecords %%a subdomain /type A >temp.txt FINDSTR /L "old.ip.add.ress" temp.txt IF NOT errorlevel 1 ( dnscmd dns.yourdomain.net /recorddelete %%a subdomain A old.ip.add.ress /f dnscmd dns.yourdomain.net /recordadd %%a subdomain A new.ip.add.ress ))
Now, if you’re like me and don’t script windows shell every day (more like once every 5 years for me), I found SS64.com a tremendous help in finding the syntax for the commands available.
But what the preceding script does is loops over every line in the zones.txt, breaking it into four tokens named %%a, %%b, %%c, %%d, using ‘ ‘ (space) as a delimiter. Of which we are only interested in %%a; which will be the zone name. We then Enumerate Records in the zone %%a with the record name ’subdomain’ (change this to whatever you are looking for) with type being ‘A’ into a file we call temp.txt. Next we look to see if there was a result for the ’subdomain’ with our “old.ip.add.ress” (again change this to the IP address you need to change). If FINDSTR does not return an errorlevel of 1, we’ve found an A Record called subdomain with the old.ip.add.ress, so let’s change it. As you can see we run two dnscmd’s to RecordDelete and then RecordAdd. That’s it!
This script worked like a charm for some recent changes we had to make and I just wanted to share it so others may benefit from it. I’d also like to hear feedback if there are more elegant solutions.
No related posts.