是否有.NET界面来编辑Windows DNS服务器区域?

时间:2011-05-26 19:08:48

标签: .net windows api dns

我认为我可能需要创建一个工具来帮助迁移DNS信息,为我们的2003和2008服务器林中的NAT'ed addreses创建“阴影”区域等。

我知道有access Windows DNS via WMI (and thus through PowerShell, WSH, etc.的方法,但.NET有更好的高级选项吗?

我会考虑更灵活的语言(例如Python)和库(例如BIND),除了微软的“安全更新”要求。

3 个答案:

答案 0 :(得分:3)

以下帖子对您有帮助吗?它提供了一些代码来处理与MS DNS服务器的通信。

http://social.msdn.microsoft.com/forums/en-US/csharpgeneral/thread/d7af771b-7c6b-4ee8-acfb-28a12bc1c5e7/

为了便于参考,将下面的C#粘贴在下面。请注意,如果您在DNS本身上本地运行此代码,则需要在调用此代码时删除下面的身份验证凭据部分...(即DnsProvider dns = new DnsProvider("localhost",null,null);

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
}
}

答案 1 :(得分:0)

我还发现了一个PowerShell module called DnsShell,它将WMI调用包装在易于使用的PS cmdlet中,甚至得到write-up from Microsoft's Scripting Guys

我还没有使用它,但如果/当我回到这个项目时,这对我来说可能是最好的工具......

答案 2 :(得分:0)

我希望解决类似的代码挑战,我在CodePlex找到了这个项目:

http://dnsapi.codeplex.com/

虽然尚未完全实施,但似乎更完整。