views:

28

answers:

1

I can create a new zone, add and delete records for that zone, all relatively easily using WMI and System.Management, but for the life of me can't figure out how to delete a zone. It doesn't appear to be a method in the WMI Documentation:

http://msdn.microsoft.com/en-us/library/ms682123(VS.85).aspx

Any thoughts on how to do this? Trying to keep the DNS server clean when we remove old website customers, but I can only get as good as deleting all the records in a zone.

EDIT: This is on a windows server 2008 R2 machine. And I would be ok with an answer of "don't use WMI" if there is an alternate solution I can execute from a remote machine and code in c#

+1  A: 

You can delete zones in the same manner you would a record.

internal static bool DeleteZoneFromDns(string ZoneName)
    {
        try
        {
            string Query = "SELECT * FROM MicrosoftDNS_Zone WHERE ContainerName = '" + ZoneName + "'";
            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();
            }
            return true;
        }
        catch (Exception)
        {
            return false;
        }
    }
mcass20
You are a rockstar! I am not sure what the DnsProvider class is, but I was able to replace that with the ManagementScope object I was currently using in my other DNS management methods. Anyway... worked first try! Thanks a million
Chris
You're welcome! Sorry for not clarifying the DnsProvider class, its a class that I wrote to assist in DNS management. I knew you'd figure it out from what I gave you.
mcass20