Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

WMI DNS delete Zone c#

Posted on 2010-08-31
11
Medium Priority
?
1,893 Views
Last Modified: 2013-11-08
Hi

Have used code from another post at Experts-Exchange (thanks)  to try to delete  unneccessary zones as the domains have expired. As no way to delete a zone file have used code as suggested but it is not fully working for me!

Have changed code slighly and used WHERE domainname = "domainsrequriedtobedeleted.com' and not WHERE OwnerName = 'an.owner.name' ";

As the SOA record still remains after running the code although A,CNAME and NS records have been deleted - does anybody know why and how to delete it?

also the code takes a long time to run 20 seconds to delete records for 1 domains?

Rgds
stephen
string query = "SELECT * FROM MicrosoftDNS_ResourceRecord WHERE OwnerName = 'an.owner.name' ";
            ManagementObjectSearcher searcher = new ManagementObjectSearcher(_scope, new ObjectQuery(query));
            ManagementObjectCollection collection = searcher.Get();
            foreach (ManagementObject o in collection)
            {
                o.Delete();
            }

Open in new window

0
Comment
Question by:stephenwilde
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
11 Comments
 
LVL 71

Expert Comment

by:Chris Dent
ID: 33565064
You can't delete the SOA without deleting the zone. You'd have to search through the return from MicrosoftDNS_Zone, find the zone in question and call the Delete method as you have above on it.

If you're deleting the zone entirely there's no point in enumerating and deleting records so you may as well save yourself that time. The WMI interface isn't the best in the world and ResourceRecord encompasses all records of all types and includes the cache, it will never be fast.

Chris
0
 

Author Comment

by:stephenwilde
ID: 33565097
Chris

Thanks very much for your quick response but can you expand on which return value from MicrosoftDNS_Zone and what you then do with the return value to delete zone and SOA please.

Rgds
Stephen
0
 
LVL 71

Expert Comment

by:Chris Dent
ID: 33565166

Something like this. You should, of course, test this carefully before use.

Chris
using System.Management;

// ...

String ServerName = "yourserver";
String Name = "thezone.com";

String WqlQuery = String.Format("SELECT * FROM MicrosoftDNS_Zone WHERE ContainerName LIKE '{0}'", Name);

ManagementScope Scope = new ManagementScope(
  String.Format(@"\\{0}\root\MicrosoftDNS\MicrosoftDNS_Zone", ServerName),
  this.Options);

ObjectQuery Query = new ObjectQuery(QueryString);

ManagementObjectSearcher wmiSearch = new ManagementObjectSearcher(Scope, Query);
ManagementObjectCollection wmiResults = wmiSearch.Get();

foreach (ManagementObject wmiZone in wmiResults) {
  // This is the Zone ManagementObject. Make sure it's the right one:

  Console.WriteLine("Zone Name: " + (String)wmiZone.Properties["Name"].Value);

  // Only if this works... delete
  // wmiZone.Delete();
}

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 71

Expert Comment

by:Chris Dent
ID: 33565214

I neglected to define this.Options, sorry, reassembled this from my management tools. This will do unless you need to authenticate the connection:


ConnectionOptions Options = new ConnectionOptions();


Just remove "this" :)

Chris
0
 

Author Comment

by:stephenwilde
ID: 33565480
Chris

Thanks for the example code but it seems to be runing for several minutes with no activity for deleting one zone.

In the where clause do I need a '% before and/or after %' the domain name ?

Rgds
Stephen
0
 
LVL 71

Expert Comment

by:Chris Dent
ID: 33565544
You don't if the domain name is exact, please feel free to change it to direct comparison rather than LIKE with wildcards (where % the any-string wildcard, what you'd normally see as *):

SELECT * FROM MicrosoftDNS_Zone WHERE ContainerName='WhateverZone'

You might also add a filter for zone type, it's Numeric if a little tricky since the values aren't consistent between DNS server versions.

e.g.

SELECT * FROM MicrosoftDNS_Zone WHERE ContainerName='WhateverZone' AND ZoneType=1

Where 1 is a Primary Zone.

It's imperative that you do this in C#? I only ask because while my management tools are written in C# they're written for PowerShell:

http://code.msdn.microsoft.com/dnsshell

I don't think the source code will make the easiest set of sample code to work with, but the PowerShell module should be perfectly capable of this operation.

Chris
0
 

Author Comment

by:stephenwilde
ID: 33566052
Chris

Using c# in .net as want to access the SQL database to list the domains for deletion.

I set up DNS records in code and assume the containername is same as domain name but is there a way to check from DNS interface ?

Rgds
stephen
0
 
LVL 71

Expert Comment

by:Chris Dent
ID: 33566078

Ahh okay.

What would you like to check? Deletion of the zone should be reflected by the interface. The snippet above should delete the zone, or are you still suffering from speed issues?

Chris
0
 

Author Comment

by:stephenwilde
ID: 33566624
Chris

Code still running for last hour and has not finished deleting the single zone - have debugged several times and it seems the ManagementObjectCollection wmiResults = wmiSearch.Get();
is not returning any records ?

Rgds
stephen
0
 
LVL 71

Accepted Solution

by:
Chris Dent earned 2000 total points
ID: 33566781

You'd have to check the filter, make it as simple as possible first. e.g.

SELECT * FROM MicrosoftDNS_Zone

Check that returns zones before filtering and before deleting.

Chris
0
 

Author Closing Comment

by:stephenwilde
ID: 33605939
Chris
Sorry for delay in closing this but had network issues that were making it difficult to find why not working.

Have not been able to get it working but I think its my network issues - it appears your solution is correct but unable to verify as times out on delete on my network
Rgds
stephen
0

Featured Post

Introducing the WatchGuard 420 Access Point

WatchGuard's newest access point includes an 802.11ac Wave 2 chipset, providing the fastest speeds for VoIP, video and music streaming, and large data file transfers. Additionally, enjoy the benefits of strong security as the 3rd radio delivers dedicated WIPS protection!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Resolve DNS query failed errors for Exchange
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question