tags:

views:

215

answers:

2

I am building a C# web app to manage our DNS servers and am using the WMI Namespace for everything. The only thing I am having trouble with is deleting 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 is: ManagementException was caught "Generic Failure". I've read online where people are deleting domains by using the zone namespace but that only works if the domain you want to delete is a zone itself. I need to delete domains that are not zones. Can anyone help?

+1  A: 

You can try with the script DnsResource.vbs from Delete a Resource Record. It uses only DNS WMI Provider. So if it will work for your porpoise you can do the same in your C# program.

You can also consider to use DnsModifyRecordsInSet. In Windos SDK (C:\Program Files\Microsoft SDKs\Windows\v7.1\Samples\netds\dns\modifyrecords) you can find an example in C++ which use DnsModifyRecordsInSet. It demonstrate how to add a record in the DNS. If you use the second parameter pDeleteRecords instead of the first pAddRecords you will be able to delete any record in the DNS.

Oleg
Thanks for trying but I don't have any problems deleting resource records. I am having a problem deleting domains.
mcass20
@mcass20: the resource type can be any from the `DNS_RECORD` struct (see http://msdn.microsoft.com/en-us/library/ms682082(VS.85).aspx) inclusive 'A' record, 'NS' record and 'CNAME' record or sometimes 'MX' record and 'SOA' recored which are mostly used to define a subdomain (see http://manual.intl.indoglobal.com/ch03s05.html). Just look as DNS Server how the subdomain is defined in your environment and delete the corresponding records.
Oleg
@Oleg: Thanks again for your insight, however, deleting the records in the domain doesn't delete the domain container itself which is my goal here.
mcass20
+2  A: 

I have not found a way to delete a domain using WMI and also checked into a Powershell snapin called DNSShell but it doesn't look like there is a command to delete the domain.

RJ
@RJ: As much as I don't want to accept your answer, it looks like you have come the closest to the correct answer.
mcass20