How to delete a DNS Zone with WMI
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 开发者_JAVA百科is an alternate solution I can execute from a remote machine and code in c#
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;
}
}
精彩评论