Manage DNS server by C# code

后端 未结 3 1795
无人及你
无人及你 2020-12-09 17:41

I need some sample code to create/delete zone and A record in microsoft DNS server by C#

3条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-09 18:12

    I agreed with Taylor but in my case i have got 2 different error with above code 1- Generic Error 2- Not Found error

    Below code has solved this problems

    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;
            }
    

提交回复
热议问题