C#利用WMI操作DNS服务器(可远程操作,需要相应权限)
C#利用WMI操作DNS服务器(可远程操作,需要相应权限)
发布时间:2016-12-28 来源:查字典编辑
摘要:usingSystem;usingSystem.Collections.Generic;usingSystem.Text;usingSyst...

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Text;

usingSystem.Data;

namespaceYaosansi

{

classTest

{

staticvoidMain()

{

MyDnsTEST();

}

///<summary>

///MyDnsTEST功能测试

///C#利用WMI操作DNS服务器(可远程操作,需要相应权限)

///Author:yaosansi

///CreateDate:2005-09-07

///ModifyDate:2006-10-25

///Site:http://www.yaosansi.com/

///E-mail:yaosansiat126dotcom

///http://www.yaosansi.com/blog/article.asp?id=935

///http://yaosansi.cnblogs.com/archive/2006/11/04/DNSServiceInCsharpWithWMI.html

///注意:此版本为WINDOWS2003DNS服务器专用.不适合其它版本操作系统.

///</summary>

staticvoidMyDnsTEST()

{

Yaosansi.Net.MYDNSdns=newYaosansi.Net.MYDNS();

//===========================================

//不对以下三个属性赋值默认DNS服务器为本机.

dns.ServerName="202.96.64.68";

dns.userName="Administrator";

dns.passWord="123456789";

//===========================================

//dns.CreateZone("yaosansi.com");

//dns.DelZone("yaosansi.com");

//dns.CreateAType("yaosansi.com","www","2.2.2.2","3600");

//dns.ModifyAType("yaosansi.com","www","127.21.0.1","800");

//dns.DelAType("yaosansi.com","mail");

//dns.CreateMXType("mail","yaosansi.com","5.5.5.5","20","3600");

//dns.ModifyMXType("mail","yaosansi.com","36000","218.1.1.1","26");

//dns.DelMXType("mail","yaosansi.com");

//dns.CreateCNAMEType("mpq2","yaosansi.com","www.yaosansi.com","3900");

//dns.ModifyCNAMEType("mpq2","abc.com","30520","www.yaosansi.com.");

//dns.DelCNAMEType("mpq","yaosansi.com");

//DataTabletable=dns.ListExistsMXType("yaosansi.com");

DataTabletable=dns.ListExistsAType("yaosansi.com");

//DataTabletable=dns.ListExistsCNAMEType("yaosansi.com");

Yaosansi.Data.DataHelp.PrintTable(table);

if(!string.IsNullOrEmpty(dns.ErrMessage))

{

Console.WriteLine("--------------------------------------------------");

Console.WriteLine("返回信息:"+dns.ErrMessage);

Console.WriteLine("--------------------------------------------------");

}

Console.WriteLine("");

Console.WriteLine("===End===");

Console.ReadLine();

}

}

}

usingSystem;

usingSystem.Management;

usingSystem.Data;

namespaceYaosansi.Net

{

///<summary>

///C#利用WMI操作DNS服务器(可远程操作,需要相应权限)

///Author:yaosansi

///CreateDate:2005-09-07

///ModifyDate:2006-10-25

///Site:http://www.yaosansi.com/

///E-mail:yaosansiat126dotcom

///http://www.yaosansi.com/blog/article.asp?id=935

///http://yaosansi.cnblogs.com/archive/2006/11/04/DNSServiceInCsharpWithWMI.html

///注意:此版本为WINDOWS2003DNS服务器专用.不适合其它版本操作系统.

///</summary>

publicclassMYDNS

{

//要连接的DNS服务器

privatestringsServerPath;

//用户名

privatestringusername=null;

//密码

privatestringpassword=null;

//服务器名称或IP地址

privatestringDNSName=null;

//表示管理操作的范围.这里是用来执行DNS的命名空间

privateManagementScopeDNS;

//用于返回检索的ManagementObject集合

privateSystem.Management.ManagementObjectCollectionQ;

//

privateManagementClassDnsClass;

//

privateManagementBaseObjectMI;

///<summary>

///返回的操作信息.

///</summary>

privatestringerrMessage=null;

///<summary>

///获取错误信息.

///</summary>

publicstringErrMessage

{

get

{

returnerrMessage;

}

}

///<summary>

///设置DNS服务器名称或IP地址

///</summary>

publicstringServerName

{

set

{

this.sServerPath=string.Format(@"{0}rootMicrosoftDNS",value);

this.DNSName=value;

}

}

///<summary>

///设置连接服务器的用户名(指定服务器IP,用户和密码后将自动连接远程服务器,本机不需要指定)

///</summary>

publicstringuserName

{

set

{

this.username=value;

}

}

///<summary>

///设置连接服务器的密码(指定服务器IP,用户和密码后将自动连接远程服务器,本机不需要指定)

///</summary>

publicstringpassWord

{

set

{

this.password=value;

}

}

///<summary>

///构造函数

///</summary>

publicMYDNS()

{

sServerPath=@"localhostrootMicrosoftDNS";

DNSName="localhost";

}

///<summary>

///建立对象.连接

///</summary>

///<paramname="DnsType">DNS类型MicrosoftDNS_Zone等</param>

privatevoidCreate(stringDnsType)

{

if(!string.IsNullOrEmpty(username)&&!string.IsNullOrEmpty(password))

{

System.Management.ConnectionOptionsConn=newConnectionOptions();

Conn.Username=username;//用户名

Conn.Password=password;//口令

DNS=newManagementScope(sServerPath,Conn);

}

else

{

DNS=newManagementScope(sServerPath);

}

if(!DNS.IsConnected)

{

DNS.Connect();

}

ManagementPathPath=newManagementPath(DnsType);

this.DnsClass=newManagementClass(DNS,Path,null);

}

///<summary>

///查询DNS并建立对象

///</summary>

///<paramname="query">WQL查询语句</param>

///<paramname="DnsType">DNS类型MicrosoftDNS_Zone等</param>

///<returns></returns>

publicManagementObjectCollectionQueryDNS(stringquery,stringDnsType)

{

this.Create(DnsType);

System.Management.ManagementObjectSearcherQS=newManagementObjectSearcher(DNS,newObjectQuery(query));

QS.Scope=DNS;

returnQS.Get();

}

///<summary>

///仅查询DNS

///</summary>

///<paramname="query">WQL查询语句</param>

///<returns></returns>

publicManagementObjectCollectionQueryDNS(stringquery)

{

if(!string.IsNullOrEmpty(username)&&!string.IsNullOrEmpty(password))

{

System.Management.ConnectionOptionsConn=newConnectionOptions();

Conn.Username=username;//用户名

Conn.Password=password;//口令

DNS=newManagementScope(sServerPath,Conn);

}

else

{

DNS=newManagementScope(sServerPath);

}

if(!DNS.IsConnected)

{

DNS.Connect();

}

System.Management.ManagementObjectSearcherQS=newManagementObjectSearcher(DNS,newObjectQuery(query));

QS.Scope=DNS;

returnQS.Get();

}

///<summary>

///判断区域是否存在

///</summary>

///<paramname="domain">区域名称</param>

///<returns></returns>

publicboolIsExistsZone(stringdomain)

{

try

{

Q=QueryDNS("Select*FromMicrosoftDNS_ZONEwhereContainerName='"+domain+"'");

foreach(ManagementObjectoManObjectinQ)

{

//Console.WriteLine(oManObject["ContainerName"].ToString());

returntrue;

}

returnfalse;

}

catch(Exceptione)

{

errMessage=e.Message;

Console.WriteLine(e.ToString());

returnfalse;

}

}

///<summary>

///创建一个新的区域,仅区域名称

///</summary>

///<paramname="domain">区域的名称</param>

publicboolCreateZone(stringdomain)

{

try

{

this.Create("MicrosoftDNS_Zone");

//如果区域已经存在

if(IsExistsZone(domain))

{

errMessage="域:"+domain+"已经存在.";

returnfalse;

}

//建立新的区域

this.MI=DnsClass.GetMethodParameters("CreateZone");

this.MI["ZoneName"]=domain;

this.MI["ZoneType"]=0;

ManagementBaseObjectOutParams=this.DnsClass.InvokeMethod("CreateZone",MI,null);

errMessage="域:"+domain+"创建成功.";

returntrue;

}

catch(Exceptione)

{

errMessage=e.Message;

Console.WriteLine(e.ToString());

returnfalse;

}

}

///<summary>

///创建一个区域,包括其它参数

///</summary>

///<paramname="domain">要创建的区域名称</param>

///<paramname="ZoneType">Typeofzone.Validvaluesarethefollowing:0Primaryzone.1Secondaryzone.2Stubzone.3Zoneforwarder.</param>

///<paramname="DataFileName">[in,optional]Nameofthedatafileassociatedwiththezone</param>

///<paramname="IpAddr">[in,optional]IPaddressofthematerDNSServerforthezone.</param>

///<paramname="AdminEmailName">[in,optional]Emailaddressoftheadministratorresponsibleforthezone</param>

///<returns></returns>

publicboolCreateZone(stringdomain,uintZoneType,stringDataFileName,string[]IpAddr,stringAdminEmailName)

{

try

{

this.Create("MicrosoftDNS_Zone");

//如果区域已经存在

if(IsExistsZone(domain))

{

errMessage="域:"+domain+"已经存在.";

returnfalse;

}

//建立新的区域

MI=DnsClass.GetMethodParameters("CreateZone");

MI["ZoneName"]=domain;

MI["ZoneType"]=ZoneType;

MI["DataFileName"]=DataFileName;

MI["IpAddr"]=IpAddr;

MI["AdminEmailName"]=AdminEmailName;

ManagementBaseObjectOutParams=this.DnsClass.InvokeMethod("CreateZone",MI,null);

errMessage="域:"+domain+"创建成功.";

returntrue;

}

catch(Exceptione)

{

errMessage=e.Message;

Console.WriteLine(e.ToString());

returnfalse;

}

}

///<summary>

///修改区域

///</summary>

///<paramname="domain">要修改的区域名称</param>

///<paramname="ZoneType">Typeofzone.Validvaluesarethefollowing:0Primaryzone.1Secondaryzone.2Stubzone.3Zoneforwarder.</param>

///<paramname="DataFileName">[in,optional]Nameofthedatafileassociatedwiththezone</param>

///<paramname="IpAddr">[in,optional]IPaddressofthematerDNSServerforthezone.</param>

///<paramname="AdminEmailName">[in,optional]Emailaddressoftheadministratorresponsibleforthezone</param>

///<returns></returns>

publicboolChangeZoneType(stringdomain,uintZoneType,stringDataFileName,string[]IpAddr,stringAdminEmailName)

{

try

{

Q=QueryDNS("Select*FromMicrosoftDNS_ZONEwhereContainerName='"+domain+"'","MicrosoftDNS_Zone");

foreach(ManagementObjectoManObjectinQ)

{

MI=oManObject.GetMethodParameters("ChangeZoneType");

MI["ZoneType"]=ZoneType;

MI["DataFileName"]=DataFileName;

MI["IpAddr"]=IpAddr;

MI["AdminEmailName"]=AdminEmailName;

oManObject.InvokeMethod("ChangeZoneType",MI,null);

errMessage="域:"+domain+"修改成功.";

returntrue;

}

errMessage="未找到域:"+domain;

returnfalse;

}

catch(Exceptione)

{

errMessage=e.Message;

Console.WriteLine(e.ToString());

returnfalse;

}

}

///<summary>

///删除区域

///</summary>

///<paramname="domain">要册除的区域的名称</param>

///<returns></returns>

publicboolDelZone(stringdomain)

{

try

{

Q=QueryDNS("Select*FromMicrosoftDNS_ZONEwhereContainerName='"+domain+"'","MicrosoftDNS_Zone");

foreach(ManagementObjectoManObjectinQ)

{

oManObject.Delete();

errMessage="域:"+domain+"删除成功.";

returntrue;

}

errMessage="未找到域:"+domain;

returnfalse;

}

catch(Exceptione)

{

errMessage=e.Message;

Console.WriteLine(e.ToString());

returnfalse;

}

}

///<summary>

///判断在某MicrosoftDNS_AType是否在指定的域中存在

///</summary>

///<paramname="domain"></param>

///<paramname="OwnerName"></param>

///<returns></returns>

publicboolIsExistsAType(stringdomain,stringOwnerName)

{

try

{

Q=QueryDNS("Select*FromMicrosoftDNS_ATypewhereOwnerName='"+OwnerName+"'andContainerName='"+domain+"'");

foreach(ManagementObjectoManObjectinQ)

{

//Console.WriteLine(oManObject["OwnerName"].ToString());

returntrue;

}

returnfalse;

}

catch(Exceptione)

{

errMessage=e.Message;

Console.WriteLine(e.ToString());

returnfalse;

}

}

///<summary>

///创建MicrosoftDNS_AType

///</summary>

///<paramname="ContainerName">NameoftheContainerfortheZone,Cache,orRootHintsinstancewhichcontainsthisRR</param>

///<paramname="HostName">主机名[如果为空或NULL,主机名将与域名保持一致.]</param>

///<paramname="TTL">Time,inseconds,thattheRRcanbecachedbyaDNSresolver</param>

///<paramname="IPAddress">StringrepresentingtheIPv4addressofthehost</param>

///<returns></returns>

publicboolCreateAType(stringContainerName,stringHostName,stringIPAddress,stringTTL)

{

try

{

stringOwnerName=null;

if(string.IsNullOrEmpty(HostName))

{

OwnerName=ContainerName;

}

else

{

OwnerName=HostName+"."+ContainerName;

}

this.Create("MicrosoftDNS_AType");

//如果区域不存在

if(!IsExistsZone(ContainerName))

{

Console.WriteLine("区域:{0}不存在,创建失败",ContainerName);

errMessage=string.Format("区域:{0}不存在,创建失败",ContainerName);

returnfalse;

}

if(IsExistsAType(ContainerName,OwnerName))

{

Console.WriteLine("{0}中已存在{1},创建失败",ContainerName,OwnerName);

errMessage=string.Format("{0}中已存在{1},创建失败",ContainerName,OwnerName);

returnfalse;

}

MI=DnsClass.GetMethodParameters("CreateInstanceFromPropertyData");

MI["DnsServerName"]="localhost";

MI["ContainerName"]=ContainerName;

MI["OwnerName"]=OwnerName;

MI["IPAddress"]=IPAddress;

if(string.IsNullOrEmpty(TTL))

{

TTL="3600";

}

MI["TTL"]=TTL;

DnsClass.InvokeMethod("CreateInstanceFromPropertyData",MI,null);

errMessage="A记录:"+OwnerName+"创建成功.";

returntrue;

}

catch(Exceptione)

{

errMessage=e.Message;

Console.WriteLine(e.ToString());

returnfalse;

}

}

///<summary>

///创建MicrosoftDNS_AType

///</summary>

///<paramname="DnsServerName">FQDNorIPaddressoftheDNSServerthatcontainsthisRR</param>

///<paramname="ContainerName">NameoftheContainerfortheZone,Cache,orRootHintsinstancewhichcontainsthisRR</param>

///<paramname="HostName">主机名[如果为空或NULL,主机名将与域名保持一致.]</param>

///<paramname="RecordClass">ClassoftheRR.Defaultvalueis1.Thefollowingvaluesarevalid.1IN(Internet)2CS(CSNET)3CH(CHAOS)4HS(Hesiod)</param>

///<paramname="TTL">Time,inseconds,thattheRRcanbecachedbyaDNSresolver</param>

///<paramname="IPAddress">StringrepresentingtheIPv4addressofthehost</param>

///<returns></returns>

publicboolCreateAType(stringDnsServerName,stringContainerName,stringHostName,uintRecordClass,uintTTL,stringIPAddress)

{

try

{

stringOwnerName=null;

if(string.IsNullOrEmpty(HostName))

{

OwnerName=ContainerName;

}

else

{

OwnerName=HostName+"."+ContainerName;

}

this.Create("MicrosoftDNS_AType");

//如果区域不存在

if(!IsExistsZone(ContainerName))

{

Console.WriteLine("区域:{0}不存在,创建失败",ContainerName);

errMessage=string.Format("区域:{0}不存在,创建失败",ContainerName);

returnfalse;

}

if(IsExistsAType(ContainerName,OwnerName))

{

Console.WriteLine("{0}中已存在{1},创建失败",ContainerName,OwnerName);

errMessage=string.Format("{0}中已存在{1},创建失败",ContainerName,OwnerName);

returnfalse;

}

MI=DnsClass.GetMethodParameters("CreateInstanceFromPropertyData");

MI["DnsServerName"]=DnsServerName;

MI["ContainerName"]=ContainerName;

MI["OwnerName"]=OwnerName;

MI["RecordClass"]=RecordClass;

MI["TTL"]=TTL;

MI["IPAddress"]=IPAddress;

DnsClass.InvokeMethod("CreateInstanceFromPropertyData",MI,null);

errMessage="A记录:"+OwnerName+"创建成功.";

returntrue;

}

catch(Exceptione)

{

errMessage=e.Message;

Console.WriteLine(e.ToString());

returnfalse;

}

}

///<summary>

///修改MicrosoftDNS_AType

///</summary>

///<paramname="ContainerName">NameoftheContainerfortheZone,Cache,orRootHintsinstancewhichcontainsthisRR</param>

///<paramname="HostName">主机名[如果为空或NULL,主机名将与域名保持一致.]</param>

///<paramname="TTL">Time,inseconds,thattheRRcanbecachedbyaDNSresolver</param>

///<paramname="IPAddress"></param>

///<returns></returns>

publicboolModifyAType(stringContainerName,stringHostName,stringIPAddress,stringTTL)

{

try

{

stringOwnerName=null;

if(string.IsNullOrEmpty(HostName))

{

OwnerName=ContainerName;

}

else

{

OwnerName=HostName+"."+ContainerName;

}

//如果区域不存在

if(!IsExistsZone(ContainerName))

{

Console.WriteLine("区域:{0}不存在,修改失败",ContainerName);

errMessage=string.Format("区域:{0}不存在,修改失败",ContainerName);

returnfalse;

}

if(!IsExistsAType(ContainerName,OwnerName))

{

Console.WriteLine("{0}中不存在{1},修改失败",ContainerName,OwnerName);

errMessage=string.Format("{0}中不存在{1},修改失败",ContainerName,OwnerName);

returnfalse;

}

Q=QueryDNS("Select*FromMicrosoftDNS_ATypewhereContainerName='"+ContainerName+"'andOwnerName='"+OwnerName+"'","MicrosoftDNS_AType");

foreach(ManagementObjectoManObjectinQ)

{

//foreach(PropertyDatapinoManObject.Properties)

//{

//try

//{Console.WriteLine(p.Name+"="+oManObject[p.Name]);}

//catch

//{}

//}

if(oManObject["IPAddress"].ToString()==IPAddress)

{

errMessage="A记录:"+OwnerName+"修改失败,必须修改IP地址.";

returnfalse;

}

MI=oManObject.GetMethodParameters("Modify");

MI["IPAddress"]=IPAddress;

MI["TTL"]=TTL;

oManObject.InvokeMethod("Modify",MI,null);

errMessage="A记录:"+OwnerName+"修改成功.";

returntrue;

}

errMessage=string.Format("{0}中不存在{1},修改失败",ContainerName,OwnerName);

returnfalse;

}

catch(Exceptione)

{

errMessage=e.Message;

Console.WriteLine(e.ToString());

returnfalse;

}

}

///<summary>

///删除MicrosoftDNS_AType

///</summary>

///<paramname="ContainerName">NameoftheContainerfortheZone,Cache,orRootHintsinstancewhichcontainsthisRR</param>

///<paramname="HostName">主机名[如果为空或NULL,主机名将与域名保持一致.]</param>

///<returns></returns>

publicboolDelAType(stringContainerName,stringHostName)

{

try

{

stringOwnerName=null;

if(string.IsNullOrEmpty(HostName))

{

OwnerName=ContainerName;

}

else

{

OwnerName=HostName+"."+ContainerName;

}

//如果区域不存在

if(!IsExistsZone(ContainerName))

{

Console.WriteLine("区域:{0}不存在,删除失败",ContainerName);

errMessage=string.Format("区域:{0}不存在,删除失败",ContainerName);

returnfalse;

}

if(!IsExistsAType(ContainerName,OwnerName))

{

Console.WriteLine("{0}中不存在{1},删除失败",ContainerName,OwnerName);

errMessage=string.Format("{0}中不存在{1},删除失败",ContainerName,OwnerName);

returnfalse;

}

Q=QueryDNS("Select*FromMicrosoftDNS_ATypewhereContainerName='"+ContainerName+"'andOwnerName='"+OwnerName+"'","MicrosoftDNS_AType");

foreach(ManagementObjectoManObjectinQ)

{

oManObject.Delete();

errMessage="A记录:"+OwnerName+"删除成功.";

returntrue;

}

errMessage=string.Format("{0}中不存在{1},删除失败",ContainerName,OwnerName);

returnfalse;

}

catch(Exceptione)

{

errMessage=e.Message;

Console.WriteLine(e.ToString());

returnfalse;

}

}

///<summary>

///列出某域名下的所有A记录.

///</summary>

///<paramname="ContainerName">NameoftheContainerfortheZone,Cache,orRootHintsinstancewhichcontainsthisRR</param>

///<returns></returns>

publicDataTableListExistsAType(stringContainerName)

{

Yaosansi.IO.Loglog=newYaosansi.IO.Log();

log.WirteLogTime();

log.WirteLog(ContainerName);

DataTabletable=newDataTable("MicrosoftDNS_AType"+ContainerName);

table.Columns.Add("主机名(A)");

table.Columns.Add("IP地址");

table.Columns.Add("TTL");

try

{

Q=QueryDNS("Select*FromMicrosoftDNS_ATypewhereContainerName='"+ContainerName+"'");

foreach(ManagementObjectoManObjectinQ)

{

try

{

DataRowrow=table.NewRow();

row["主机名(A)"]=oManObject["OwnerName"];

row["IP地址"]=oManObject["IPAddress"];

row["TTL"]=oManObject["TTL"];

table.Rows.Add(row);

}

catch(Exceptione){log.WirteLog(e.ToString());}

}

}

catch(Exceptione)

{

errMessage=e.Message;

Console.WriteLine(e.ToString());

log.WirteLog(e.ToString());

}

Yaosansi.Data.DataHelp.PrintTableLog(table);

returntable;

}

///<summary>

///列出某域名下的所有MX记录.

///</summary>

///<paramname="ContainerName">NameoftheContainerfortheZone,Cache,orRootHintsinstancewhichcontainsthisRR</param>

///<returns></returns>

publicDataTableListExistsMXType(stringContainerName)

{

DataTabletable=newDataTable("MicrosoftDNS_MXType"+ContainerName);

table.Columns.Add("邮件交换记录(MX)");

table.Columns.Add("目标主机");

table.Columns.Add("优先级");

table.Columns.Add("TTL");

try

{

Q=QueryDNS("Select*FromMicrosoftDNS_MXTypewhereContainerName='"+ContainerName+"'");

foreach(ManagementObjectoManObjectinQ)

{

try

{

DataRowrow=table.NewRow();

row["目标主机"]=oManObject["MailExchange"];

row["邮件交换记录(MX)"]=oManObject["OwnerName"];

row["优先级"]=oManObject["Preference"];

row["TTL"]=oManObject["TTL"];

table.Rows.Add(row);

}

catch{}

}

}

catch(Exceptione)

{

errMessage=e.Message;

Console.WriteLine(e.ToString());

}

returntable;

}

///<summary>

///列出某域名下的所有别名.

///</summary>

///<paramname="ContainerName">NameoftheContainerfortheZone,Cache,orRootHintsinstancewhichcontainsthisRR</param>

///<returns></returns>

publicDataTableListExistsCNAMEType(stringContainerName)

{

DataTabletable=newDataTable("MicrosoftDNS_CNAMEType"+ContainerName);

table.Columns.Add("别名(CNAME)");

table.Columns.Add("别名主机");

table.Columns.Add("TTL");

try

{

Q=QueryDNS("Select*FromMicrosoftDNS_CNAMETypewhereContainerName='"+ContainerName+"'");

foreach(ManagementObjectoManObjectinQ)

{

try

{

DataRowrow=table.NewRow();

row["别名(CNAME)"]=oManObject["OwnerName"];

row["别名主机"]=oManObject["PrimaryName"];

row["TTL"]=oManObject["TTL"];

table.Rows.Add(row);

}

catch{}

}

}

catch(Exceptione)

{

errMessage=e.Message;

Console.WriteLine(e.ToString());

}

returntable;

}

///<summary>

///判断在某MicrosoftDNS_MXType是否在指定的域中存在

///</summary>

///<paramname="ContainerName">主域名主域NameoftheContainerfortheZone,Cache,orRootHintsinstancethatcontainsthisRR.</param>

///<paramname="OwnerName">OwnernamefortheRR.</param>

///<returns></returns>

publicboolIsExistsMXType(stringContainerName,stringOwnerName)

{

try

{

Q=QueryDNS("Select*FromMicrosoftDNS_MXTypeWhereContainerName='"+ContainerName+"'andOwnerName='"+OwnerName+"'");

foreach(ManagementObjectoManObjectinQ)

{

//Console.WriteLine(oManObject["MailExchange"].ToString());

returntrue;

}

returnfalse;

}

catch(Exceptione)

{

errMessage=e.Message;

Console.WriteLine(e.ToString());

returnfalse;

}

}

///<summary>

///创建MicrosoftDNS_MXType记录(邮件交换记录)

///</summary>

///<paramname="HostName">主机名[如果为空或NULL,主机名将与域名保持一致.]</param>

///<paramname="ContainerName">主域NameoftheContainerfortheZone,Cache,orRootHintsinstancethatcontainsthisRR.</param>

///<paramname="MailExchange">目标主机FQDNspecifyingahostwillingtoactasamailexchangefortheownername</param>

///<paramname="Preference">优先级PreferencegiventothisRRamongothersatthesameowner.Lowervaluesarepreferred</param>

///<paramname="TTL">Time,inseconds,thattheRRcanbecachedbyaDNSresolver</param>

///<returns></returns>

publicboolCreateMXType(stringHostName,stringContainerName,stringMailExchange,stringPreference,stringTTL)

{

try

{

stringOwnerName=null;

if(string.IsNullOrEmpty(HostName))

{

OwnerName=ContainerName;

}

else

{

OwnerName=HostName+"."+ContainerName;

}

this.Create("MicrosoftDNS_MXType");

//如果区域不存在

if(!IsExistsZone(ContainerName))

{

Console.WriteLine("区域:{0}不存在,创建失败",ContainerName);

errMessage=string.Format("区域:{0}不存在,创建失败",ContainerName);

returnfalse;

}

if(IsExistsMXType(ContainerName,OwnerName))

{

Console.WriteLine("{0}中已存在{1},创建失败",ContainerName,OwnerName);

errMessage=string.Format("{0}中已存在{1},创建失败",ContainerName,OwnerName);

returnfalse;

}

MI=DnsClass.GetMethodParameters("CreateInstanceFromPropertyData");

MI["DnsServerName"]="localhost";

MI["ContainerName"]=ContainerName;

//MI["RecordClass"]=1;//Defaultvalueis1.//1IN(Internet)//2CS(CSNET)//3CH(CHAOS)//4HS(Hesiod)

MI["MailExchange"]=MailExchange;

MI["OwnerName"]=OwnerName;

MI["Preference"]=Preference;

if(string.IsNullOrEmpty(TTL))

{

TTL="3600";

}

MI["TTL"]=TTL;

DnsClass.InvokeMethod("CreateInstanceFromPropertyData",MI,null);

errMessage="MX记录:"+OwnerName+"创建成功.";

returntrue;

}

catch(Exceptione)

{

errMessage=e.Message;

Console.WriteLine(e.ToString());

returnfalse;

}

}

///<summary>

///修改MicrosoftDNS_MXType记录(修改邮件交换记录)

///</summary>

///<paramname="HostName">主机名[如果为空或NULL,主机名将与域名保持一致.]</param>

///<paramname="ContainerName">主域名主域NameoftheContainerfortheZone,Cache,orRootHintsinstancethatcontainsthisRR.</param>

///<paramname="TTL">[in,optional]Time,inseconds,thattheRRcanbecachedbyaDNSresolver.</param>

///<paramname="MailExchange">[in,optional]FQDNspecifyingahostwillingtoactasamailexchangefortheownername.</param>

///<paramname="Preference">邮件优先级[in,optional]PreferencegiventothisRRamongothersatthesameowner.Lowervaluesarepreferred.</param>

///<returns></returns>

publicboolModifyMXType(stringHostName,stringContainerName,stringTTL,stringMailExchange,stringPreference)

{

try

{

stringOwnerName=null;

if(string.IsNullOrEmpty(HostName))

{

OwnerName=ContainerName;

}

else

{

OwnerName=HostName+"."+ContainerName;

}

this.Create("MicrosoftDNS_MXType");

//如果区域不存在

if(!IsExistsZone(ContainerName))

{

Console.WriteLine("区域:{0}不存在,修改失败",ContainerName);

errMessage=string.Format("区域:{0}不存在,修改失败",ContainerName);

returnfalse;

}

if(!IsExistsMXType(ContainerName,OwnerName))

{

Console.WriteLine("{0}中不存在{1},修改失败",ContainerName,OwnerName);

errMessage=string.Format("{0}中不存在{1},修改失败",ContainerName,OwnerName);

returnfalse;

}

Q=QueryDNS("Select*FromMicrosoftDNS_MXTypeWhereContainerName='"+ContainerName+"'andOwnerName='"+OwnerName+"'");

foreach(ManagementObjectoManObjectinQ)

{

MI=oManObject.GetMethodParameters("Modify");

if(string.IsNullOrEmpty(TTL))

{

TTL="3600";

}

if(ClearEndDot(oManObject["MailExchange"].ToString())==ClearEndDot(MailExchange)&&oManObject["Preference"].ToString()==Preference)

{

errMessage="MX记录:"+OwnerName+"修改失败,必须修改目标主机或邮件优先级.";

returnfalse;

}

MI["TTL"]=TTL;

MI["MailExchange"]=MailExchange;

MI["Preference"]=Preference;

oManObject.InvokeMethod("Modify",MI,null);

errMessage="MX记录:"+OwnerName+"修改成功.";

returntrue;

}

returnfalse;

}

catch(Exceptione)

{

errMessage=e.Message;

Console.WriteLine(e.ToString());

returnfalse;

}

}

///<summary>

///删除MicrosoftDNS_MXType

///</summary>

///<paramname="HostName">主机名[如果为空或NULL,主机名将与域名保持一致.]</param>

///<paramname="ContainerName">主域名主域NameoftheContainerfortheZone,Cache,orRootHintsinstancethatcontainsthisRR.</param>

///<returns></returns>

publicboolDelMXType(stringHostName,stringContainerName)

{

try

{

stringOwnerName=null;

if(string.IsNullOrEmpty(HostName))

{

OwnerName=ContainerName;

}

else

{

OwnerName=HostName+"."+ContainerName;

}

this.Create("MicrosoftDNS_MXType");

//如果区域不存在

if(!IsExistsZone(ContainerName))

{

Console.WriteLine("区域:{0}不存在,删除失败",ContainerName);

errMessage=string.Format("区域:{0}不存在,删除失败",ContainerName);

returnfalse;

}

if(!IsExistsMXType(ContainerName,OwnerName))

{

Console.WriteLine("{0}中不存在{1},删除失败",ContainerName,OwnerName);

errMessage=string.Format("{0}中不存在{1},删除失败",ContainerName,OwnerName);

returnfalse;

}

Q=QueryDNS("Select*FromMicrosoftDNS_MXTypeWhereContainerName='"+ContainerName+"'andOwnerName='"+OwnerName+"'");

foreach(ManagementObjectoManObjectinQ)

{

oManObject.Delete();

errMessage="MX记录:"+OwnerName+"删除成功.";

returntrue;

}

returnfalse;

}

catch(Exceptione)

{

errMessage=e.Message;

Console.WriteLine(e.ToString());

returnfalse;

}

}

///<summary>

///判断在某MicrosoftDNS_CNAMEType是否在指定的域中存在

///</summary>

///<paramname="ContainerName">主域名主域NameoftheContainerfortheZone,Cache,orRootHintsinstancethatcontainsthisRR.</param>

///<paramname="OwnerName">OwnernamefortheRR.</param>

///<returns></returns>

publicboolIsExistsCNAMEType(stringContainerName,stringOwnerName)

{

try

{

Q=QueryDNS("Select*FromMicrosoftDNS_CNAMETypeWhereContainerName='"+ContainerName+"'andOwnerName='"+OwnerName+"'");

foreach(ManagementObjectoManObjectinQ)

{

//Console.WriteLine(oManObject["MailExchange"].ToString());

returntrue;

}

returnfalse;

}

catch(Exceptione)

{

errMessage=e.Message;

Console.WriteLine(e.ToString());

returnfalse;

}

}

///<summary>

///创建MicrosoftDNS_CNAMEType记录(别名)

///</summary>

///<paramname="HostName">主机名[如果为空或NULL,主机名将与域名保持一致.]</param>

///<paramname="ContainerName">主域NameoftheContainerfortheZone,Cache,orRootHintsinstancethatcontainsthisRR.</param>

///<paramname="PrimaryName">in]PrimarynameoftheCNAMERR</param>

///<paramname="TTL">Time,inseconds,thattheRRcanbecachedbyaDNSresolver</param>

///<returns></returns>

publicboolCreateCNAMEType(stringHostName,stringContainerName,stringPrimaryName,stringTTL)

{

try

{

stringOwnerName=null;

if(string.IsNullOrEmpty(HostName))

{

OwnerName=ContainerName;

}

else

{

OwnerName=HostName+"."+ContainerName;

}

this.Create("MicrosoftDNS_CNAMEType");

//如果区域不存在

if(!IsExistsZone(ContainerName))

{

Console.WriteLine("区域:{0}不存在,创建失败",ContainerName);

errMessage=string.Format("区域:{0}不存在,创建失败",ContainerName);

returnfalse;

}

if(IsExistsCNAMEType(ContainerName,OwnerName))

{

Console.WriteLine("{0}中已存在{1},创建失败",ContainerName,OwnerName);

errMessage=string.Format("{0}中已存在{1},创建失败",ContainerName,OwnerName);

returnfalse;

}

MI=DnsClass.GetMethodParameters("CreateInstanceFromPropertyData");

MI["DnsServerName"]="localhost";

MI["ContainerName"]=ContainerName;

//MI["RecordClass"]=1;//Defaultvalueis1.//1IN(Internet)//2CS(CSNET)//3CH(CHAOS)//4HS(Hesiod)

MI["PrimaryName"]=PrimaryName;

MI["OwnerName"]=OwnerName;

if(string.IsNullOrEmpty(TTL))

{

TTL="3600";

}

MI["TTL"]=TTL;

DnsClass.InvokeMethod("CreateInstanceFromPropertyData",MI,null);

errMessage="CNAME:"+OwnerName+"创建成功.";

returntrue;

}

catch(Exceptione)

{

errMessage=e.Message;

Console.WriteLine(e.ToString());

returnfalse;

}

}

///<summary>

///修改MicrosoftDNS_CNAMEType记录(别名)

///</summary>

///<paramname="HostName">主机名[如果为空或NULL,主机名将与域名保持一致.]</param>

///<paramname="ContainerName">主域名主域NameoftheContainerfortheZone,Cache,orRootHintsinstancethatcontainsthisRR.</param>

///<paramname="TTL">[in,optional]Time,inseconds,thattheRRcanbecachedbyaDNSresolver.</param>

///<paramname="PrimaryName">in]PrimarynameoftheCNAMERR</param>

///<returns></returns>

publicboolModifyCNAMEType(stringHostName,stringContainerName,stringTTL,stringPrimaryName)

{

try

{

stringOwnerName=null;

if(string.IsNullOrEmpty(HostName))

{

OwnerName=ContainerName;

}

else

{

OwnerName=HostName+"."+ContainerName;

}

this.Create("MicrosoftDNS_CNAMEType");

//如果区域不存在

if(!IsExistsZone(ContainerName))

{

Console.WriteLine("区域:{0}不存在,修改失败",ContainerName);

errMessage=string.Format("区域:{0}不存在,修改失败",ContainerName);

returnfalse;

}

if(!IsExistsCNAMEType(ContainerName,OwnerName))

{

Console.WriteLine("{0}中不存在{1},修改失败",ContainerName,OwnerName);

errMessage=string.Format("{0}中不存在{1},修改失败",ContainerName,OwnerName);

returnfalse;

}

Q=QueryDNS("Select*FromMicrosoftDNS_CNAMETypeWhereContainerName='"+ContainerName+"'andOwnerName='"+OwnerName+"'");

foreach(ManagementObjectoManObjectinQ)

{

if(ClearEndDot(oManObject["PrimaryName"].ToString())==ClearEndDot(PrimaryName))

{

errMessage="CName记录:"+OwnerName+"修改失败,必须修改别名主机.";

returnfalse;

}

MI=oManObject.GetMethodParameters("Modify");

if(string.IsNullOrEmpty(TTL))

{

TTL="3600";

}

MI["TTL"]=TTL;

MI["PrimaryName"]=PrimaryName;

oManObject.InvokeMethod("Modify",MI,null);

errMessage="CNAME:"+OwnerName+"修改成功.";

returntrue;

}

returnfalse;

}

catch(Exceptione)

{

errMessage=e.Message;

Console.WriteLine(e.ToString());

returnfalse;

}

}

///<summary>

///删除MicrosoftDNS_CNAMEType

///</summary>

///<paramname="HostName">主机名[如果为空或NULL,主机名将与域名保持一致.]</param>

///<paramname="ContainerName">主域名主域NameoftheContainerfortheZone,Cache,orRootHintsinstancethatcontainsthisRR.</param>

///<returns></returns>

publicboolDelCNAMEType(stringHostName,stringContainerName)

{

try

{

stringOwnerName=null;

if(string.IsNullOrEmpty(HostName))

{

OwnerName=ContainerName;

}

else

{

OwnerName=HostName+"."+ContainerName;

}

this.Create("MicrosoftDNS_CNAMEType");

//如果区域不存在

if(!IsExistsZone(ContainerName))

{

Console.WriteLine("区域:{0}不存在,删除失败",ContainerName);

errMessage=string.Format("区域:{0}不存在,删除失败",ContainerName);

returnfalse;

}

if(!IsExistsCNAMEType(ContainerName,OwnerName))

{

Console.WriteLine("{0}中不存在{1},删除失败",ContainerName,OwnerName);

errMessage=string.Format("{0}中不存在{1},删除失败",ContainerName,OwnerName);

returnfalse;

}

Q=QueryDNS("Select*FromMicrosoftDNS_CNAMETypeWhereContainerName='"+ContainerName+"'andOwnerName='"+OwnerName+"'");

foreach(ManagementObjectoManObjectinQ)

{

oManObject.Delete();

errMessage="CNAME:"+OwnerName+"删除成功.";

returntrue;

}

returnfalse;

}

catch(Exceptione)

{

errMessage=e.Message;

Console.WriteLine(e.ToString());

returnfalse;

}

}

///<summary>

///去除以.结尾的字符串的.

///</summary>

///<paramname="str"></param>

///<returns></returns>

privatestringClearEndDot(stringstr)

{

stringreturnStr=str;

if(!string.IsNullOrEmpty(str))

{

intl=str.LastIndexOf(".");

if(l!=-1&&l==str.Length-1)

{

returnStr=str.Substring(0,str.Length-1);

}

}

returnreturnStr;

}

}

}

推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
相关阅读
网友关注
最新C#教程学习
热门C#教程学习
编程开发子分类