Kirk Evans Blog

.NET From a Markup Perspective

Create DNS Entries From a File

Create DNS Entries From a File

Rate This
  • Comments 3

This post will show how to create DNS entries from a file using the DNSCmd.exe tool.

Background

I have a kind of edge case that I am trying to troubleshoot for my customer.  They have a lot of sub-companies, and they created a web application per company.  Fast forward a few years, they now have 143 web applications.  For those keeping score, that’s 44 more than the maximum supported limit for SharePoint.  We are working to collapse those web applications to host-named site collections.  In order to test the process, I needed to implement a tool where I could create that many web applications in my environment.  The first challenge was creating that many DNS A records.

Implementation

After searching the web and coming up fairly empty on doing this with WMI or other technologies, I decided to take the hack way out and use DNSCmd.exe.  This utility is present on the server when DNS is installed on the server.  It becomes a pretty trivial exercise, then, to write a program that runs DNSCmd.exe, provides the necessary command line arguments, and generates the DNS entries.

I put all of my new DNS entries into a file, with each new host name on a single line:

CompanyA
CompanyB
CompanyC
CompanyD

Then, it’s just a matter of reading the file and using the System.Diagnostics.Process.Start() method, providing the right command line arguments.

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Diagnostics;

namespace CreateDNSEntries
{
    class Program
    {
        static void Main(string[] args)
        {
            CreateDNSENtries();
        }

        static void CreateDNSENtries()
        {
            string[] sites = File.ReadAllLines("C:\\temp\\dnsnames.txt");
            foreach (string site in sites)
            {         
                string passRaw = "YourPasswordGoesHere";

                System.Security.SecureString pass = new System.Security.SecureString();
                for (int i = 0; i < passRaw.Length; i++)
                {
                    pass.AppendChar(passRaw[i]);
                }

                Process.Start("dnscmd.exe", "/recordadd sharepoint.com " 
                    + site + " A 192.168.2.5", "administrator", pass, "sharepoint");                
            }
        }
    }
}

The command that we are running uses the /recordadd command line argument, this is the stuff you will probably want to change.

  • Domain: sharepoint.com
  • Host: site
  • Record type: A
  • IP address: 192.168.2.5

We also use the domain administrator’s credentials to run the utility, using the System.Security.SecureString class to provide the password.

Run your new command line utility on your DNS server to add the entries.  Simple, huh?  It took me longer to fail searching for an answer than it did to whip together a hack solution.  Sometimes it’s faster to just roll up your sleeves.

 

For More Information

DNSCmd Syntax

SecureString Class

Process.Start Method

  • Cool article. What about PowerShell? I am sure you may have already looked into.

  • Thanks, Prashant!  Duh, you're right... it's even easier with PowerShell's Start-Process.  technet.microsoft.com/.../dd347667.aspx Thanks for the tip, you can tell that my PowerShell skills are still pretty weak.  That would have saved me even more time!

  • The PowerShell for it would look like:

    Get-Content "C:\temp\dnsnames.txt" | %{start-process dnscmd.exe -ArgumentList ("/recordadd sharepoint.com " $_  " A 192.168.2.5")}

Page 1 of 1 (3 items)
Leave a Comment
  • Please add 5 and 2 and type the answer here:
  • Post
Translate This Page
Search
Archive
Archives