How to remove a DNS zone using WMI

I can create a new zone, add and remove entries for this zone, everything is relatively easy using WMI and System.Management, but for the rest of my life I can’t figure out how to delete a zone. This is not like the 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 delete old website clients, but I can only get that I delete all the records in the zone.

EDIT: This is on a Windows Server 2008 R2 computer. And I would be fine with the answer "do not use WMI" if there is an alternative solution that I can execute from a remote machine and code in C #

+3
source share
1 answer

You can delete zones in the same way as recording.

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;
        }
    }
+3
source

Source: https://habr.com/ru/post/1767286/


All Articles