Scripting DNS entries


For reasons to do with DR we needed to change our IP addressing scheme, either for our servers or for our PCs and printers and what not. Obviously changing PCs and printers was a whole lot easier due to the possible issues of inept developers’ hard coding IP addresses causing no end of problems with re-addressing the servers.

This has entailed changing all the DNS entries for our printers, (whatever happened to the paperless office?) That in itself is no real challenge, they all have web interfaces so a quick connect and amend the setting then next printer.
Updating the DNS entries was going to be a bit tiresome so I did a quick Google on how to do this with a script and up popped DNSCMD.

You can run this from a CMD box on your PC, the main options you need to know are /recorddelete and /recordadd.

To delete a record you simple need to type

dnscmd <ServerName> /recorddelete <ZoneName> <NodeName> <RRType> <RRData>[/f]

What does all that mean?

Well if you have DNS record on DNS server called DNS01 for a printer called HP1 in the zone company.com and the record type is A and IP address of 10.0.0.1, then to delete it type

dnscmd DNS01 /recorddelete company.com HP1 A 10.0.0.1

And its job done. 
"But what's that /F thingy at the end for?" you may well ask. Well if you include that it won't ask if you’re sure you want to do this, if you knock up a batch file with 50 or so entries in it you'll need that at the end or it will fail. This will remove the PTR record as well.

To add new entries in you just do a 

dnscmd [<ServerName>] /recordadd <ZoneName> <NodeName> <RRType> <RRData>

Or with the same details as above

dnscmd DNS01 /recordadd company.com HP1 A 10.0.0.1

There is no /f switch needed here.
If you need to create a PTR record at the same time you can, all the stuff on the web suggest running a second /record add with the reverse look up zone and a PTR as the record type. Something like

dnscmd DC01 /recordadd 0.0.10.in-addr.arpa. 10 PTR HP1.company.com

However there is a much easier way, you can simply add the /CreatePTR switch to the original command line so it looks like

dnscmd DNS01 /recordadd company.com HP1 /CreatePTR A 10.0.0.1

This will add a normal resource record and the PTR record at the same time; it’s like a buy one, get one free offer at the local supermarket.

There is a lot of stuff you can do with DNSCMD a good place to look is the Microsoft Technet article at http://technet.microsoft.com/en-us/library/cc772069%28WS.10%29.aspx#BKMK_15. This is actually under the AZ list of command line references for server 2008/2008R2 so you may wish to do some book marking. Some other ones that are worth a look are DSADD, DSQuery, DSMOD for doing stuff in AD and another really useful one is FORFILES that allows you to carry out an action on a set of files that meet certain criteria, I use this to delete files older than a set number days in a particular folder running from task scheduler, it is handy way to stop those IIS log files getting out of hand, though you could move them to another drive or anything you can run from the commend line such as zipping them up.

Comments

  1. The above commands errors out with the below error:

    Command failed: DNS_WARNING_PTR_CREATE_FAILED 9715 0x25F3

    ReplyDelete
  2. Sorry it didn't work for you. I assume it was teh pointer creation that failed looking at the error message. WIthout knowing your environment or the exact command line you entered it is difficult to know why it failed. have you tried bith of the pointer creation commands listed?

    ReplyDelete
  3. Is there a way to remove both the record and its corresponding PTR in the reverse lookup zone with a single command?

    I mean can I run something like

    dnscmd DNS01 /recordDelete company.com HP1 /removePTR A 10.0.0.1

    ReplyDelete
  4. any update on deleting both records in one command?

    ReplyDelete
  5. dnscmd DNS01 /recordDelete company.com HP1 A 10.0.0.1

    deletes both A and PTR

    ReplyDelete

Post a Comment

Popular posts from this blog

Enterprise Vault - Failed Exchange Task

Windows Phone to iPhone - a painful transition