.net - 是否有用于编辑 Windows DNS 服务器区域的 .NET 界面?

标签 .net windows api dns

<分区>

我想我可能需要创建一个工具来协助迁移 DNS 信息,为 NAT 地址创建“影子”区域,等等,在我们的 2003 和 2008 服务器林中。

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

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

最佳答案

以下帖子对您有帮助吗?它提供了一些代码来处理与 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
}
}

关于.net - 是否有用于编辑 Windows DNS 服务器区域的 .NET 界面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6143771/

相关文章:

api - 传递的数组包含子程序中预期的更多元素

.net - 什么是托管 C++ 等效于 C# using 语句

c# - P4Api client.GetFileMappings 不返回分支文件的前导减号/破折号

javascript - 在不同的方法中再次使用 componentDidMount 的 fetch 方法

c# - 无法使用 prnmngr.vbs 在远程计算机上设置默认打印机

linux - 针对共享库的内存写保护

javascript - 使用通知 API 时如何向通知添加 URL?

.net - 将 WCF .NET 3.5 发布到 IIS 6 (Windows Server 2003)

.net - SSIS - 从文件名中获取子字符串并插入到数据库表中

windows - 在 Windows 中的 Swipl prolog 中清除屏幕