Dnscmd - Managing the Windows DNS Server

This page explains how you can script the creation of new domains in a Windows DNS server using the dnscmd utility.

For one of my projects, I needed to find a way to redirect requests from local machines to for example google.com to a local webserver. To accomplish this, I setup a DNS server, made sure that all the local machines were using this DNS and added google.com in this DNS server to redirect to my local server.

Since I needed to do this for more than 1000 domains, I wanted to find a way to script this. I found that Windows 2003 ships with a tool called 'Dnscmd'. This is a tool you can use from the command line, so you can create a DOS batch file to automatically add multiple domains to the Windows Server 2003 DNS Server. Below you can find how I added the different domains to the DNS server using the Dnscmd command line tool.

The Dnscmd tool is not installed by default, but you can find it on your Windows Server 2003 CD in the following directory:
Windows CD -> Support -> Tools -> Support.cab

For the purpose I needed the Dnscmd, I only needed 2 of the command-parameters, but you can find the complete Dnscmd syntax on Microsoft Technet.

  1. First you need to add a new zone to the DNS server. The zone we want to add is www.example.com and we want to add this as a primary zone. In our case, the Dnscmd command becomes:
    Dnscmd /zoneadd www.example.com /DsPrimary
  2. Second you need to add a recode for the zone we just added. To make sure that we only want to have www.example.com who's referencing to our internal IP 192.168.1.25, we used the '@' sign. I don't remember where I found this, but if you don't add that, you won't be able to resolve other example.com subdomains.
    Dnscmd /RecordAdd www.example.com @ A 192.168.1.25

From the site of Microsoft, we added the complete syntax description of the command we just used:

  • Dnscmd zoneadd
    Adds a zone to the DNS server.

    Syntax
    dnscmd [ServerName] /zoneadd ZoneName ZoneType [/dp FQDN| {/domain|/enterprise|/legacy}]

    Parameters

    • ServerName: Specifies the DNS server the administrator plans to manage, represented by IP address, FQDN, or Host name. If omitted, the local server is used.
    • ZoneName: Specifies the name of the zone.
    • ZoneType: Specifies the type of zone to create. Each type has different required parameters. /dsprimaryCreates an Active Directory-integrated zone./primary /file FileNameCreates a standard primary zone and specifies the name of the file that will store the zone information./secondary MasterIPAddress [MasterIPAddress...]Creates a standard secondary zone./stub MasterIPAddress [MasterIPAddress...] /fileFileNameCreates a file-backed stub zone./dsstub MasterIPAddress [MasterIPAddress...]Creates an Active Directory-integrated stub zone./forwarder MasterIPAddress [MasterIPAddress]... /fileFileNameSpecifies that the created zone forwards unresolved queries to another DNS server./dsforwarderSpecifies that the created Active Directory-integrated zone forwards unresolved queries to another DNS server.
    • /dp FQDN {/domain | /enterprise | /legacy}: Specifies the directory partition on which to store the zone.FQDNSpecifies fully qualified domain name of the directory partition./domainStores the zone on the domain directory partition./enterpriseStores the zone on the enterprise directory partition./legacyStores the zone on a legacy directory partition.

    Remarks
    Specifying a zone type of /forwarder or /dsforwarder creates a zone that performs conditional forwarding.

    Sample Usage
    dnscmd dnssvr1.contoso.com /zoneadd test.contoso.com /dsprimary
    dnscmd dnssvr1.contoso.com /zoneadd secondtest.contoso.com /secondary 10.0.0.2

  • Dnscmd recordadd
    Adds a record to a specified zone in a DNS server.

    Syntax
    Art Imagednscmd [ServerName] /recordadd ZoneName NodeName RRType RRData

    Parameters

    • 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.

    Note
    When you add a record, make sure you use the correct data type and data format. For a list of resource record types and the appropriate data types, see Resource records reference.

    Sample Usage
    dnscmd dnssvr1.contoso.com /recordadd test A 10.0.0.5
    dnscmd /recordadd test.contoso.com test MX 10 mailserver.test.contoso.com

Tags: 

You might also be interested in...