none
Adding DNS Zones and A Records on a Remote DNS Server RRS feed

  • General discussion

  • Hi everyone,

    Just recently I had to make a class to add dns zones and a records to a remote dns server. It was very anoying to create because the info was hard to find - also I couldnt find any C# examples so I had to convert the examples I found from VB or VBS.

    So here is my final product. The entire class code is below. You can use this to establish a connection to a remote server and then add/remove dns zone and a-records or to check and see if a particular domain namespace is already in use.


    using System;
    using System.Management;
    namespace DNS
    {
    public class DnsProvider
    {
    #region Members
    private ManagementScope Session=null;
    public string Server=null;
    public string User=null;
    private string Password=null;
    private string m_NameSpace=null;
    #endregion
    public DnsProvider(string serverName,string userName,string password)
    {
    this.Server=serverName;
    this.User=userName;
    this.Password=password;
    this.Logon();
    this.Initialize();
    }
    private void Logon()
    {
    this.m_NameSpace="\\\\" + this.Server + "\\root\\microsoftdns";
    ConnectionOptions con=new ConnectionOptions();
    con.Username=this.User;
    con.Password=this.Password;
    con.Impersonation=ImpersonationLevel.Impersonate;
    this.Session=new ManagementScope(this.NameSpace);
    this.Session.Options=con;
    this.Session.Connect();
    }
    private void Initialize()
    {
    }
    #region Methods
    public void Dispose()
    {
    }
    public void Dispose(ref ManagementClass x)
    {
    if(x!=null)
    {
    x.Dispose();
    x=null;
    }
    }
    public void Dispose(ref ManagementBaseObject x)
    {
    if(x!=null)
    {
    x.Dispose();
    x=null;
    }
    }
    public bool DomainExists(string domainName)
    {
    bool retval=false;
    string wql="";
    wql="SELECT *";
    wql+=" FROM MicrosoftDNS_ATYPE";
    wql+=" WHERE OwnerName = '" + domainName + "'";
    ObjectQuery q=new ObjectQuery(wql);
    ManagementObjectSearcher s=new ManagementObjectSearcher(this.Session,q);
    ManagementObjectCollection col=s.Get();
    int total=col.Count;
    foreach(ManagementObject o in col)
    {
    retval=true;
    }
    return retval;
    }
    public void AddDomain(string domainName,string ipDestination)
    {
    //check if domain already exists
    if(this.DomainExists(domainName))
    {
    throw new Exception("The domain you are trying to add already exists on this server!");
    }
    //generate zone
    ManagementClass man=this.Manage("MicrosoftDNS_Zone");
    ManagementBaseObject ret=null;
    ManagementBaseObject obj=man.GetMethodParameters("CreateZone");
    obj["ZoneName"]=domainName;
    obj["ZoneType"]=0;
    //invoke method, dispose unneccesary vars
    man.InvokeMethod("CreateZone",obj,null);
    this.Dispose(ref obj);
    this.Dispose(ref ret);
    this.Dispose(ref man);
    //add rr containing the ip destination
    this.AddARecord(domainName,null,ipDestination);
    }
    public void RemoveDomain(string domainName)
    {
    string wql="";
    wql="SELECT *";
    wql+=" FROM MicrosoftDNS_Zone";
    wql+=" WHERE Name = '" + domainName + "'";
    ObjectQuery q=new ObjectQuery(wql);
    ManagementObjectSearcher s=new ManagementObjectSearcher(this.Session,q);
    ManagementObjectCollection col=s.Get();
    int total=col.Count;
    foreach(ManagementObject o in col)
    {
    o.Delete();
    }
    }
    public void AddARecord(string domain,string recordName,string ipDestination)
    {
    if(this.DomainExists(recordName + "." + domain))
    {
    throw new Exception("That record already exists!");
    }
    ManagementClass man=new ManagementClass(this.Session,new ManagementPath("MicrosoftDNS_ATYPE"),null);
    ManagementBaseObject vars=man.GetMethodParameters("CreateInstanceFromPropertyData");
    vars["DnsServerName"]=this.Server;
    vars["ContainerName"]=domain;
    if(recordName==null)
    {
    vars["OwnerName"]=domain;
    }
    else
    {
    vars["OwnerName"]=recordName + "." + domain;
    }
    vars["IPAddress"]=ipDestination;
    man.InvokeMethod("CreateInstanceFromPropertyData",vars,null);
    }
    public void RemoveARecord(string domain,string aRecord)
    {
    string wql="";
    wql="SELECT *";
    wql+=" FROM MicrosoftDNS_ATYPE";
    wql+=" WHERE OwnerName = '" + aRecord + "." + domain + "'";
    ObjectQuery q=new ObjectQuery(wql);
    ManagementObjectSearcher s=new ManagementObjectSearcher(this.Session,q);
    ManagementObjectCollection col=s.Get();
    int total=col.Count;
    foreach(ManagementObject o in col)
    {
    o.Delete();
    }
    }
    #endregion
    #region Properties
    public string NameSpace
    {
    get
    {
    return this.m_NameSpace;
    }
    }
    public bool Enabled
    {
    get
    {
    bool retval=false;
    try
    {
    SelectQuery wql=new SelectQuery();
    wql.QueryString="";
    }
    catch
    {}
    return retval;
    }
    }
    public ManagementClass Manage(string path)
    {
    //ManagementClass retval=new ManagementClass(path);
    ManagementClass retval=new ManagementClass(this.Session,new ManagementPath(path),null);
    return retval;
    }
    #endregion
    }
    }
    Friday, June 9, 2006 2:36 PM

All replies

  • How can I change or append to your code to allow it to work for localhost changes to DNS? I keep getting an error saying: "User credentials cannot be used for local connections" but I need to be able to do both local and remote changes. I haven't done any programming with WMI but my boss gave me a project that requires it make DNS changes. I'm beginning to understand, but I can't find anything for how to allow remote connections.

    Wednesday, March 19, 2008 7:36 PM
  • How can I change or append to your code to allow it to work for localhost changes to DNS? I keep getting an error saying: "User credentials cannot be used for local connections" but I need to be able to do both local and remote changes. I haven't done any programming with WMI but my boss gave me a project that requires it make DNS changes. I'm beginning to understand, but I can't find anything for how to allow remote connections.


    As the error states, "credentials cannot be used for local connections".  Omit them:

    public DnsProvider(string serverName,string userName,string password)
    {
    this.Server=serverName;
    //this.User=userName;
    //this.Password=password;
    this.Logon();
    this.Initialize();
    }

    Monday, October 26, 2009 6:03 PM
  • Or simply null those values while creating the object:
    DnsProvider dns = new DnsProvider("localhost",null,null);

    Best,
    Luis
    Monday, January 18, 2010 7:14 PM
  • Great code, thanks for sharing :o))
    Monday, January 18, 2010 7:14 PM
  • Hey great Code Thanks a Lot.

    Now Just Want to know ,how will I enable the Ptr Record For the A Record Created using Above Codes. 


    Friday, September 3, 2010 11:12 AM
  • i am using the above code.. but i am getting an error at this line in domain exists method

    ManagementObjectCollection col=s.Get();
    int total=col.Count;

    the error is:System.Management.ManagementException: Invalid class

    can you plz tell me why am i getting this error?



    Tuesday, March 29, 2011 11:42 AM
  • Maybe something to do with the minimum supported server? http://msdn.microsoft.com/en-us/library/ms682174.aspx
    Tuesday, March 29, 2011 12:14 PM
  • Hi

    Add "System.Management" Dll To your Reference

    Saturday, July 30, 2011 8:50 AM
  • Hi

    I am Using This Class But "The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)" Error Shown.

    plz Help me


    Saturday, July 30, 2011 8:54 AM
  • That's very helpful !

    I need to Get The List Of My Domain but i cant find any code I need

    Please Help me

    Saturday, February 18, 2017 8:50 AM