Thursday, April 10, 2008

Quick and dirty way to mass update Windows 2003 DNS entries

I recently came across an issue where I wanted to add in a few hundred DNS entries into our Windows 2003 DNS server. I wanted to script this, as I have no intention of do a mass update manually and I want to re-use this in the future.

Most of the Google results I find say the easiest methods are to either convert the DNS zone to "standard primary" from "active directory integrated" and then manually editing the resulting text file created, or to use a script by Dean Wells called "dnsdump.cmd"

The problem with converting a domain to standard is, aside from being a convoluted hack, I'm updating our core AD dns zone, which obviously cannot be changed to standard without causing issues.

The problem with "dnsdump.cmd" is it requires being run from the DNS server itself and is more focused on migrating DNS information than simple updates like this.

I found VBScript examples using WMI, but in true VBScript fashion, while it works it takes 20 lines of script to do one line's worth of work.

Reading through the source code of dnsdump.cmd, I realized it was calling "dnscmd.exe", which is one of the Windows Server support tools. After installing the support tools (from "SUPPORT\TOOLS" on my Windows Server 2003 CD-ROM) I had this very nice utility. You can get its syntax by just running it from the command line. Below is a solution to my problem that only requires a text file and one call to dnscmd.exe to complete (well, I guess technically many calls, as this is a "for" loop, but you know what I mean ;-) )

My solution:
  1. Create a text file that only contains two space-separated values: the server name and the IP address. For this example its called "addresses.txt".
  2. Call dnscmd as follows:
for /f "tokens=1,2" %a in (addresses.txt) do dnscmd.exe dnsserver.mydomain.com /RecordAdd mydomain.com %a A %b

Simple and sweet :)

Note: you need to be an admin of "dnsserver.mydomain.com" as specified above. Also, for PTR records, you need to change "A" to "PTR". Just look at the output of "dnscmd.exe" by itself for help. Also, "dnscmd.exe /EnumZones" is useful for getting the exact spelling to use for the "mydomain.com" portion above.

Update: to answer a question Linkposed to me: "tokens=1,2" tells the "for" operator to return the first and second values of the delimited line in "addresses.txt". By default, spaces are a delimiter, so if the text file contains "server1.mydomain.com 1.2.3.4" then %a is "server1.mydomain.com" and %b is "1.2.3.4". You can actually use any delimiter (like a comma) and then specify such as:
for /f "tokens=1,2 delims=," %a ....
Type "for /?" from a command prompt for more details.

Update2: A nice article on dnscmd.exe is here: Scripting DNS

0 comments: