我想我可能需要創建一個工具來協助遷移DNS信息,爲我們的2003和2008服務器的林中創建「陰影」區域,用於NAT加入的內容等等。是否有.NET接口來編輯Windows DNS服務器區域?
我知道有辦法可以access Windows DNS via WMI (and thus through PowerShell, WSH, etc.,但是有更好的.NET高級選項嗎?
除了Microsoft的「安全更新」的要求之外,我會考慮更靈活的語言(例如Python)和庫(例如BIND)。
我想我可能需要創建一個工具來協助遷移DNS信息,爲我們的2003和2008服務器的林中創建「陰影」區域,用於NAT加入的內容等等。是否有.NET接口來編輯Windows DNS服務器區域?
我知道有辦法可以access Windows DNS via WMI (and thus through PowerShell, WSH, etc.,但是有更好的.NET高級選項嗎?
除了Microsoft的「安全更新」的要求之外,我會考慮更靈活的語言(例如Python)和庫(例如BIND)。
以下文章是否可以幫助你?它提供了一些代碼來處理與MS DNS服務器的通話。
粘貼低於它們的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
}
}
噢,那正是我要找的那種東西。另外,它不是「完成」,所以我得到更多的擺弄它;)感謝 - – ewall 2011-06-17 18:30:31
什麼是serverName?服務器的IP地址? – 2014-05-27 20:15:28
我還發現一個PowerShell module called DnsShell用來包裝WMI調用易於使用的PS cmdlet,它甚至得到了write-up from Microsoft's Scripting Guys。
我沒有用它的還不很多,但如果/當我回到這個項目,這可能是對我最好的工具...
請注意,有一個[類似的問題尋找DNS庫的.NET庫](http://stackoverflow.com/questions/4538490/what-dns-libraries-are-there-in-net),但不是Windows DNS服務器。 – ewall 2011-05-26 19:34:57
Bueller ...任何人? Bueller?任何人? – ewall 2011-06-08 16:07:39