How can I delete a DNS domain programmatically?

I am building a C # web application to manage our DNS servers and use the WMI namespace for everything. The only thing I came across was the removal of DNS domains. Here is my code:

internal static bool DeleteDomainFromDns(string DnsServerName, string ContainerName, string Name) { try { string Query = "SELECT * FROM MicrosoftDNS_Domain WHERE DnsServerName = '" + DnsServerName + "' AND ContainerName = '" + ContainerName + "' AND Name = '" + Name + "'"; ObjectQuery qry = new ObjectQuery(Query); DnsProvider dns = new DnsProvider(); ManagementObjectSearcher s = new ManagementObjectSearcher(dns.Session, qry); ManagementObjectCollection col = s.Get(); dns.Dispose(); foreach (ManagementObject obj in col) { obj.Delete(); //Exception occurs here } return true; } catch (Exception) { return false; } } 

The error I get: ManagementException was detected by "Generic Failure". I read on the Internet where people delete domains using the zone namespace, but this only works if the domain you want to delete is the zone itself. I need to remove domains that are not zones. Can anyone help?

+7
c # dns wmi
source share
2 answers

I did not find a way to delete the domain using WMI, and also registered with Snapin Powershell called DNSShell, but it does not seem to have a command to delete the domain.

+2
source share

You can try using script DnsResource.vbs from Delete resource record . It uses only the DNS WMI Provider. So if it works for your marine swig, you can do the same in your C # program.

You can also use DnsModifyRecordsInSet . In the Winos SDK (C: \ Program Files \ Microsoft SDK \ Windows \ v7.1 \ Samples \ netds \ dns \ changerecords) you can find a C ++ example that uses DnsModifyRecordsInSet . It shows how to add an entry in DNS. If you use the second parameter pDeleteRecords instead of the first pAddRecords , you can delete any entry in the DNS.

+1
source share

All Articles