Sunday, February 13, 2011

Adding DNS Entries in .NET

Adding DNS A, CNAME, NS, and MX records in .NET is simple when you use WMI. Using WMI, you can add records in a matter of a few seconds.  Please take a look at the code below.  Essentially with WMI you tap into a remote machine, then you Create a ManagementObject, and initiate theWMI method CreateInstanceFromPropertyData.

        Label1.Text = "";

 

                    if (txtPassword.Text.Length < 2 || txtUsername.Text.Length < 2)

 

                    {


                       
this.Label1.Style["text-align"] = "left";

 

                        Label1.ForeColor = Color.Maroon;

 

                        Label1.Text = "Please enter username and/or password. ";

 

                    }


                   
else

 
                    {


                       
string remoteMachine = txtServer.Text;

 

                       


                       
ConnectionOptions co = new ConnectionOptions();

 

                        co.Username = txtUsername.Text;


                        co.Password = txtPassword.Text;


                        System.Net.
IPHostEntry h =

 

                    System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName());

 

                        string IPAddress = h.AddressList.GetValue(0).ToString();

 

                        string lm = System.Net.Dns.GetHostName().ToString();


 

                        Label1.ForeColor = Color.Black;

 

                        co.Impersonation = ImpersonationLevel.Impersonate;

 

                        // co.Impersonation = ImpersonationLevel.

 
                        co.EnablePrivileges = true;

 

                        co.Authentication = AuthenticationLevel.PacketPrivacy;

 

                        Label1.ForeColor = Color.Black;


 

                        ConnectionOptions connOptions = new ConnectionOptions();

 

                        connOptions.Impersonation = ImpersonationLevel.Impersonate;

 

                        connOptions.EnablePrivileges = true;

 

                   


                   
try

 
                        {


                           
ManagementScope oMs = new ManagementScope("\\\\" + remoteMachine + "\\root\\microsoftdns", co);

 

                           


                         
// string queryStr = "SELECT * FROM MicrosoftDNS_AType";

 
                         //   ObjectQuery oQuery = new ObjectQuery(queryStr);

 
                         //   ManagementObjectSearcher oSearcher = new ManagementObjectSearcher(oMs, oQuery);


 

                            ObjectGetOptions objectGetOptions = new ObjectGetOptions();

 

                            string type;

 

                            ManagementPath managementPath;

 

                            managementPath = new ManagementPath("MicrosoftDNS_AType");

 

                        


                           
try

 
                            {


                                type = words[1].ToString().ToLower();


                            }


                           
catch

 
                            {


                                type =
"a";

 

                            }



                           
if (type == "ipv6" || type == "aaaa")

 

                                managementPath = new ManagementPath("MicrosoftDNS_AAAAType");

 

                            if (type == "a")

 

                                managementPath = new ManagementPath("MicrosoftDNS_AType");

 

                            if (type == "cname")

 

                                managementPath = new ManagementPath("MicrosoftDNS_CNAMEType");

 

                            if (type == "mx")

 

                                managementPath = new ManagementPath("MicrosoftDNS_MXType");

 

                            if (type == "ns")

 

                                managementPath = new ManagementPath("MicrosoftDNS_NSType");

 

                        


                           
ManagementClass oClass = new ManagementClass(oMs, managementPath, objectGetOptions);

 

                            ManagementBaseObject inParams = oClass.GetMethodParameters("CreateInstanceFromPropertyData");

 

                            ManagementBaseObject vars =

 

                                oClass.GetMethodParameters("CreateInstanceFromPropertyData");

 

                            string DN = words[2].ToString();

 

                            string IPA = words[4].ToString();

 

                            string FQDN = words[3].ToString() + "." + DN;

 

                            vars["DnsServerName"] = txtServer.Text;

 

                            vars["ContainerName"] = DN;

 

                          


                           
if (type == "a")

 

                            vars["IPAddress"] = IPA;

 

                           if (type == "cname")

 

                               vars["PrimaryName"] = IPA;

 

                           if (type == "mx")

 

                               vars["MailExchange"] = IPA;

 

                           if (type == "ns")

 

                               vars["NSHost"] = IPA;

 

                           if (type == "ipv6" || type == "aaaa")

 

                               vars["IPv6Address"] = IPA;

 

                         


                            vars[
"OwnerName"] = FQDN;    


 

                            ManagementBaseObject outParams = oClass.InvokeMethod("CreateInstanceFromPropertyData", vars, null);

 

                            Label1.Text += "DNS Entry was successfully added!<br />";


 

                            string status = "undetermined";

 

          


                                status =
"<font color='green'>Successful</font>";

 

                           


                            Label1.Text +=
"DNS " + type.ToUpper() + " entry creation: <b>" + FQDN

 

                            + "</b>" + " - " + status;

 

                            Label1.Text += "<br />";


 

                            this.Label1.Style["text-align"] = "center";

 

                           


                        }


                       
catch (Exception ex)

 

                        {


                            Label1.ForeColor =
Color.Maroon;

 

                            Label1.Text += "Coudlnt' Create Entry: " +

 

                                "May be duplicate entry or wrong information, check syntax!";

 

                        }


                    }


No comments:

Post a Comment

Generating "Always On Top" NSWindow in macOS across all detected displays

Also: Using UIKit & Cocoa Frameworks using Objective-C In m acOS or OS X , written in either Objective-C or Swift  Langues, you m...