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

时间:2021-05-20

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:///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}\root\MicrosoftDNS",value);
this.DNSName=value;
}
}

///<summary>
///设置连接服务器的用户名(指定服务器IP,用户和密码后将自动连接远程服务器,本机不需要指定)
///</summary>
publicstringuserName
{
set
{
this.username=value;
}
}

///<summary>
///设置连接服务器的密码(指定服务器IP,用户和密码后将自动连接远程服务器,本机不需要指定)
///</summary>
publicstringpassWord
{
set
{
this.password=value;
}
}

///<summary>
///构造函数
///</summary>
publicMYDNS()
{
sServerPath=@"\\localhost\root\MicrosoftDNS";
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+"删

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章