通过C#代码管理DNS服务器

时间:2011-02-28 18:55:52

标签: c# dns

我需要一些示例代码来通过C#

在Microsoft DNS服务器中创建/删除区域和A记录

3 个答案:

答案 0 :(得分:15)

您必须使用WMI来调用DNSProvider。

这是添加记录:

 public void AddARecord(string hostName, string zone, string iPAddress, string dnsServerName)
 {
      ManagementScope scope = 
         new ManagementScope(@"\\" + dnsServerName + "\\root\\MicrosoftDNS");

      scope.Connect();

      ManagementClass cmiClass =
         new ManagementClass(scope, 
                             new ManagementPath("MicrosoftDNS_AType"),
                             null);

     ManagementBaseObject inParams = 
         wmiClass.GetMethodParameters("CreateInstanceFromPropertyData");

     inParams["DnsServerName"] = this.ServerName;
     inParams["ContainerName"] = zone;
     inParams["OwnerName"] = hostName + "." + zone;
     inParams["IPAddress"] = iPAddress;

     cmiClass.InvokeMethod("CreateInstanceFromPropertyData", inParams, null);
}

您可以引用WMI引用并根据需要使用方法和类对其进行扩展 http://msdn.microsoft.com/en-us/library/ms682123(v=vs.85).aspx

答案 1 :(得分:2)

微软将其作为POX服务公开,因此你可以使用System.Net的东西和电子邮件将XML通过网络推送到它。您的用户凭据。

http://technet.microsoft.com/en-us/library/dd278634.aspx

答案 2 :(得分:0)

我同意泰勒的观点,但就我而言,以上代码有2个不同的错误 1-一般错误 2-未找到错误

下面的代码解决了这个问题

private ManagementPath UpdateARecord(string strDNSZone, string strHostName, string strIPAddress)
        {
            ManagementScope mgmtScope = new ManagementScope(@"\\.\Root\MicrosoftDNS");
            ManagementClass mgmtClass = null;
            ManagementBaseObject mgmtParams = null;
            ManagementObjectSearcher mgmtSearch = null;
            ManagementObjectCollection mgmtDNSRecords = null;
            string strQuery;

            strQuery = string.Format("SELECT * FROM MicrosoftDNS_AType WHERE OwnerName = '{0}.{1}'", strHostName, strDNSZone);

            mgmtScope.Connect();

            mgmtSearch = new ManagementObjectSearcher(mgmtScope, new ObjectQuery(strQuery));

            mgmtDNSRecords = mgmtSearch.Get();

            //// Multiple A records with the same record name, but different IPv4 addresses, skip.
            //if (mgmtDNSRecords.Count > 1)
            //{
            //    // Take appropriate action here.
            //}
            //// Existing A record found, update record.
            //else
            if (mgmtDNSRecords.Count == 1)
            {
                ManagementObject mo = new ManagementObject();
                foreach (ManagementObject mgmtDNSRecord in mgmtDNSRecords)
                {
                    if (mgmtDNSRecord["RecordData"].ToString() != strIPAddress)
                    {
                        mgmtParams = mgmtDNSRecord.GetMethodParameters("Modify");
                        mgmtParams["IPAddress"] = strIPAddress;

                        mgmtDNSRecord.InvokeMethod("Modify", mgmtParams, null);
                    }
                    mo = mgmtDNSRecord;
                    break;
                }

                return new ManagementPath(mo["RR"].ToString());
            }
            // A record does not exist, create new record.
            else
            {
                mgmtClass = new ManagementClass(mgmtScope, new ManagementPath("MicrosoftDNS_AType"), null);

                mgmtParams = mgmtClass.GetMethodParameters("CreateInstanceFromPropertyData");
                mgmtParams["DnsServerName"] = Environment.MachineName;
                mgmtParams["ContainerName"] = strDNSZone;
                mgmtParams["OwnerName"] = strDNSZone;// string.Format("{0}.{1}", strHostName.ToLower(), strDNSZone);

                mgmtParams["IPAddress"] = strIPAddress;

                var outParams = mgmtClass.InvokeMethod("CreateInstanceFromPropertyData", mgmtParams, null);

                if ((outParams.Properties["RR"] != null))
                {
                    return new ManagementPath(outParams["RR"].ToString());
                }
            }

            return null;
        }