Adding DNS Zones and A Records on a Remote DNS Server

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
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.

<hr id="[object]

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

View the full article
 
Back
Top