Werbung: SecurityConsole.de verwaltet Ihre Computer mit Security Essentails aus der Cloud!
30 Tage kostenfrei testen und 20% Rabatt für Ihre Bestellung mit Promocode: WBF2685582
(Promocode gültig bis 31.12.2011)

Group:  English: Windows Server » microsoft.public.windows.server.dns
Thread: Programmatically adding DNS zones and records

HTVi
TV Discussion Newsgroups

Programmatically adding DNS zones and records
Kumar 10/18/2008 1:37:01 AM
I am trying to the below code in my application to create a dns zone/record.
This was a sample provided as DNSprovider on the net. However, I am getting
an error when it reaches connect.



I cannot connect remotely to the DNS server. I pass the following details

DnsProvider dnsprov = new DnsProvider("10.10.10.12", "admin", "password");
dnsprov.AddDomain("testdomain.com", "10.10.10.14");
dnsprov.AddARecord("testdomain.com", "home", "10.10.10.14");
dnsprov.Dispose();
MessageBox.Show("Completed adding record");

I tried passing both user name as 'admin' and 'domain\admin'. nothing works

The issue is at the managementscope namespace and is occuring in the method
logon when it reaches

this.Session.Connect();

The error I get is

"User credentials error for local connections"

.. Somewhere something is wrong.



Below given is the code

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()
{
try
{
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();
}
catch (Exception ex)
{
System.Console.WriteLine(ex.Message);
}
}
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
}
}

Home | Search | Terms | Imprint Contact
Newsgroups Reader - provided by WiredBox.Net
Suche nach Orten, Städten, Postleitzahlen, Vorwahlen, Kfz-Kennzeichen