Fog Creek Software
Discussion Board




Welcome! and rules

Joel on Software

WMI DNS - Deletion Possible?

I have a question about working with WMI (WMI DNS Provider in specific) using C#.  Microsoft provided class references, including example scripts at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dns/dns/dns_wmi_provider.asp.  However the scripts are in vbs.  I'm working on translating the scripts to C# and building up from there.

Unfortunately, I'm stuck.  In the vbs scripts, there is a Delete_ method used to delete zones, but it doesn't show up in the reference. Is this a vbs specific command? If so, what is the C# equivalent?

Here is my translation so far:

public void createZone(string name, uint type)
{
    ManagementClass zone = new ManagementClass(scope, new ManagementPath("MicrosoftDNS_Zone"), null);
    ManagementBaseObject inputs = zone.GetMethodParameters("CreateZone");
    ManagementBaseObject outputs = null;

    inputs["ZoneName"] = name;
    inputs["ZoneType"] = type;

    try
    {
        outputs = zone.InvokeMethod("CreateZone", inputs, null);
        /* Console.WriteLine(outputs["RR"]); */
    }
    catch(Exception e)
    {
        Console.WriteLine(e.Source + " " + e.Message);
        Console.WriteLine("Likely trying to create a zone already available");
    }
}

public void deleteZone(string name, uint type)
{
    ManagementClass zone = new ManagementClass(scope, new ManagementPath("MicrosoftDNS_Zone"), null);
    MethodDataCollection methods = zone.Methods;

    foreach (MethodData method in methods)
    {
        Console.WriteLine("Method = " + method.Name);
    }
    Console.WriteLine("...how to delete zone?");

    /* zone = new ManagementClass(scope, new ManagementPath("MicrosoftDNS_Zone.ContainerName= */
}

Google doesn't shed much light since every example out there seems to be written in vbscript.

mike
Thursday, July 01, 2004

I found a better example to work with:

set objDNS = GetObject("winMgmts:\\server\root\MicrosoftDNS")
set objDNSServer = objDNS.Get("MicrosoftDNS_Server.Name="".""")
set objZones = objDNS.ExecQuery("Select * from MicrosoftDNS_Zone " & _
                                "Where DnsServerName = '" & _
                                objDNSServer.Name & "'")
WScript.Echo "Zones on " & objDNSServer.Name
for each objZone in objZones
  WScript.Echo " " & objZOne.Name
next


The line I'm having trouble converting to C# is:

set objDNSServer = objDNS.Get("MicrosoftDNS_Server.Name="".""")

I can get ManagementClass object for MicrosoftDNS_Server, but I can't figure out how to specify a server name in C#.

mike
Thursday, July 01, 2004

I was thinking to be the only one translating this in .Net.
not a lot of google help.

I found that the execquery is very buggy.

but the InstancesOf is not

iterate threw it and when you find your object call delete.

Jean-Francois Borie
Thursday, July 29, 2004

I am running across the same problem for resource records. I have a custom written vbscript that adds/deletes records, but I wanted to recode it into c#. I can do the record additions (A/CNAME, etc.), but I can't figure out how to do the deletes. If anyone has any info....please reply. Thanks.

Chris Rizzo
Friday, August 27, 2004

I figured this out...thought I would pass it along....Here is the code...

(recType is "A", or "PTR", "CNAME", etc....)

ManagementScope oMs = new ManagementScope("\\\\" + dnsServer + "\\root\\microsoftdns");
string strQuery = "select * from microsoftdns_" + recType +
"type where containername = '" + domain + "' and ownername = '" + strValue + "'";

ManagementObjectSearcher oS = new ManagementObjectSearcher(strQuery);
oS.Scope = oMs;
ManagementObjectCollection oRc = oS.Get();
foreach (ManagementObject oR in oRc)
{
oR.Delete();
}

I originally had my query too specific, but I changed it to a wildcard, and it works great now!!

Chris Rizzo
Tuesday, August 31, 2004

Chris,

In your code:

string strQuery = "select * from microsoftdns_" + recType +
"type where containername = '" + domain + "' and ownername = '" + strValue + "'";

What is the value of strValue?

I am just starting out working with WMI and like you am unable to find very much information.

Thanks for your help.

Matt.

Matthew Cantillon
Thursday, September 02, 2004

*  Recent Topics

*  Fog Creek Home