How to bulk import entries in a DNS zone on Microsoft Server

Efstratios ArvanitidisICT Manager - Fleet Division
CERTIFIED EXPERT
Published:
There have been a lot of times when we have seen the need to enter a large number of DNS entries in a forward lookup zone.

The standard procedure would be to launch the DNS Manager console, create the Zone and start adding new hosts using the New Host action. The higher the number of new hosts to be added, the greater the possibility of making a mistake. This article has been created to make the Administrator's life easier.

When the admin is executing the above GUI driven procedure, he is actually executing the dnscmd command followed by some switches and parameters. It seems reasonable to write a batch file that would do this in sequence with a hit of a single button.

The actual dnscmd command syntax is explained below:

dnscmd [ServerName] /recordadd ZoneName NodeName RRType RRData

While the Parameters are the following:
ServerName: Specifies the DNS server the administrator is planning to manage, represented by local computer syntax, IP address, FQDN, or Host name. If omitted, the local server is used.
ZoneName: Specifies the zone in which the record resides.
NodeName: Specifies a specific node in the zone.
RRType: Specifies the type of record to be added.
RRData: Specifies the type of data that is expected when using a certain data type.

Based on the above if our ServerName is dt00001.mydomain.com, our ZoneName is myzone.mydomain.com, our NodeName is dt00xxx (where 001<xxx<255), the RRType is an A record type and the RRData is 172.29.2.xxx (where 001<xxx<255). To add dt00002 host we would simple need to run the following command:
dnscmd dt00001.mydomain.com /recordadd myzone.mydomain.com dt00002 A 172.29.2.2

Open in new window

To add the dt00003 we would need to run the following command:
dnscmd dt00001.mydomain.com /recordadd myzone.mydomain.com dt00003 A 172.29.2.3

Open in new window

The above commands could be combined easily in a single .bat file that when executed one after the other would create all the needed host entries in our zone. The problem rises on how to create this .bat file easily, eliminating the possibility of a mistake. To do this all you will need is Excel and this guide.
1. Open Excel and create a new blank spreadsheet.
2. Starting on cell A1 type the above command splitting each parameter in a different cell like in the following capture First entry in Excel3. Use the automatic option that Excel provides you to extend your list vertically adding automatically the new entries as shown in the following captures Drag cell verticallyDrag Cell vertically result4. Repeat step 3 as far as you need in order to get all the entries you want until you get something like the following captureFinal list5. When done choose Save as and select CSV DOS format.
6. Browse to the location where you saved your .csv file and open it with Notepad. What you will see should be something like the following captureExported CSV7. You can see that you are nearly done. What you need now is to replace the commas followed by a space with a space character. To do that go to menu Edit and choose Replace. In the pop up window in the "Find what" field type ", " (a comma followed by a space character without the quotes) and in the "Replace with" type in " " (a space character without the quotes). Click on Replace All button. The result would be something like the following capture CSV Edited8. The final step is to rename the file from .csv to .bat and get something like the following capture Batch fileSince we are done with the creation of the batch file we should now be able to execute it on the actual DNS server. To do that follow the below steps:

1. Go to your server and launch the DNS Manager
2. Expand your Forward Lookup Zones
3. Choose New Zone from the actions menu
4. Follow the wizard without changing anything apart from adding the zone's name. Make sure to set the name to myzone.mydomain.com (similar to what you used while creating your batch file)
5. Copy the batch file to your DNS server to a location that you can easily reach (e.g. c:\)
6. Locate the Command line from your server's programs list, right click and choose to run your batch file as administrator, in order to launch it with elevated privileges.
7. In the command prompt move to C:\ (or to the exact location where you saved your batch file) and execute your file by typing its name. In our case command prompt would seem like the following captureCommand line execution8. Wait until the batch file executes all its lines and go back to your DNS Manager console. Refresh and ta da... all your hosts are in place

Now that you have the procedure in place you can always tweak your .bat file with all the parameters you want in order to delete, edit, and so on using information from Microsoft's related page.
4
27,509 Views
Efstratios ArvanitidisICT Manager - Fleet Division
CERTIFIED EXPERT

Comments (1)

Commented:
If the naming and IP scheme is as straight forward as in the example, just do it with a script loop. DOS, VBScript, or PowerShell will do. Here's an example in DOS.

Just assign the variables appropriate values. Then when you are satisfied that everything is right, remove the "echo" from the start of the dnscmd line.

-----Cut Here-----
@echo off
  cls

:SET_ENV
  SETLOCAL ENABLEDELAYEDEXPANSION

:SET_VAR
  set Count_Val=15
  set Start_Val=2
  set DNS_Server=dt00001.mydomain.com
  set Zone_Name=myzone.mydomain.com
  set Host_Prefix=dt
  set Host_Val=00000
  set IP_Network=172.29.2

:DO
  for /L %%n in (%Start_Val%,1,%Count_Val%) do (
    set Padded_Val=%Host_Val%%%n
    echo dnscmd %DNS_Server% /recordadd %Zone_Name% %Host_Prefix%!Padded_Val:~-5! A %IP_Network%.%%n
  )

:END
  pause
  cls
  exit 0
-----Cut Here-----

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.