DNS WMI Provider

  • Thread starter Thread starter Guest
  • Start date Start date
Sure. Here is DeleteZone and DeleteRR I did in c# awhile ago. HTH

public void DeleteZone(string zoneName)
{
//This method Deletes the zone named in zoneName.
//Step 1 - Check input parms:
if ( zoneName == null )
throw new ArgumentNullException("zoneName", "zoneName is null.");

try
{
ObjectQuery oQuery = new System.Management.ObjectQuery("Select * from
MicrosoftDNS_Zone Where ContainerName = '" + zoneName + "'");
ManagementObjectSearcher oSearcher = new ManagementObjectSearcher(ms,
oQuery);
ManagementObjectCollection zoneCollection = oSearcher.Get();

bool zoneFound = false;
foreach ( ManagementObject zone in zoneCollection )
{
zoneFound = true;
zone.Delete();
break; //Only one zone could match, so break.
}
if ( ! zoneFound )
throw new ZoneNotFoundException();
}
catch(System.Management.ManagementException e)
{
throw new ApplicationException(e.ErrorCode.ToString() + ". " + e.Message);
}
catch(MVPTools.DnsManagement.ZoneNotFoundException)
{
throw new ZoneNotFoundException("Zone not found.");
}
catch(Exception e)
{
throw new ApplicationException(e.Message);
}
} // End DeleteZone()

public void DeleteRR(string zoneName, string ownerName, string rData)
{
//Step 1 - Check input parms:
if ( zoneName == null )
throw new ArgumentNullException("zoneName", "zoneName is null.");

if ( ownerName == null )
throw new ArgumentNullException("ownerName", "ownerName is null.");

if ( rData == null )
throw new ArgumentNullException("rData", "rData is null.");

//Step 2 - Check for and Get RR object if it exists.
try
{
ManagementObject rr = null;
if ( ! RRExists(zoneName, ownerName, rData, out rr) )
throw new MVPTools.DnsManagement.RRNotFoundException();
if ( rr == null )
throw new RRNotFoundException();
rr.Delete();
}
catch(System.Management.ManagementException e)
{
throw new ApplicationException(e.ErrorCode.ToString() + ". " + e.Message);
}
catch(MVPTools.DnsManagement.RRNotFoundException)
{
throw new RRNotFoundException("RR not found.");
}
catch(Exception e)
{
throw new ApplicationException(e.Message);
}
} // End DeleteRR()

--
William Stacey, MVP

m8v316 said:
Is it possible to delete a zone or record using WMI?

TechNet has a quite a few easy to understand examples for creating and
enumerating zones and records at:
http://www.microsoft.com/technet/community/scriptcenter/network/default.mspx

But there is no examples to delete zones or records. Does anyone know how
its done and, better yet, have a simple example?
 
dnscmd /?

/zonedelete is one of the numerous options. You can do it in a batch file of
shell to it in VB.

--
Sincerely,

Dèjì Akómöláfé, MCSE MCSA MCP+I
Microsoft MVP - Directory Services
www.readymaids.com - COMPLETE SPAM Protection
www.akomolafe.com
Do you now realize that Today is the Tomorrow you were worried about
Yesterday? -anon


m8v316 said:
Is it possible to delete a zone or record using WMI?

TechNet has a quite a few easy to understand examples for creating and
enumerating zones and records at:
http://www.microsoft.com/technet/community/scriptcenter/network/default.mspx

But there is no examples to delete zones or records. Does anyone know how
its done and, better yet, have a simple example?
 
In
William Stacey said:
Sure. Here is DeleteZone and DeleteRR I did in c# awhile ago. HTH

public void DeleteZone(string zoneName)
{
//This method Deletes the zone named in zoneName.
//Step 1 - Check input parms:
if ( zoneName == null )
throw new ArgumentNullException("zoneName", "zoneName is null.");

try
{
ObjectQuery oQuery = new System.Management.ObjectQuery("Select *
from MicrosoftDNS_Zone Where ContainerName = '" + zoneName + "'");
ManagementObjectSearcher oSearcher = new
ManagementObjectSearcher(ms, oQuery);
ManagementObjectCollection zoneCollection = oSearcher.Get();

bool zoneFound = false;
foreach ( ManagementObject zone in zoneCollection )
{
zoneFound = true;
zone.Delete();
break; //Only one zone could match, so break.
}
if ( ! zoneFound )
throw new ZoneNotFoundException();
}
catch(System.Management.ManagementException e)
{
throw new ApplicationException(e.ErrorCode.ToString() + ". " +
e.Message); }
catch(MVPTools.DnsManagement.ZoneNotFoundException)
{
throw new ZoneNotFoundException("Zone not found.");
}
catch(Exception e)
{
throw new ApplicationException(e.Message);
}
} // End DeleteZone()

public void DeleteRR(string zoneName, string ownerName, string rData)
{
//Step 1 - Check input parms:
if ( zoneName == null )
throw new ArgumentNullException("zoneName", "zoneName is null.");

if ( ownerName == null )
throw new ArgumentNullException("ownerName", "ownerName is null.");

if ( rData == null )
throw new ArgumentNullException("rData", "rData is null.");

//Step 2 - Check for and Get RR object if it exists.
try
{
ManagementObject rr = null;
if ( ! RRExists(zoneName, ownerName, rData, out rr) )
throw new MVPTools.DnsManagement.RRNotFoundException();
if ( rr == null )
throw new RRNotFoundException();
rr.Delete();
}
catch(System.Management.ManagementException e)
{
throw new ApplicationException(e.ErrorCode.ToString() + ". " +
e.Message); }
catch(MVPTools.DnsManagement.RRNotFoundException)
{
throw new RRNotFoundException("RR not found.");
}
catch(Exception e)
{
throw new ApplicationException(e.Message);
}
} // End DeleteRR()



enumerating zones and records at:http://www.microsoft.com/technet/community/scriptcenter/network/default.mspx
its done and, better yet, have a simple example?

William and Deji, there was a question asked last week (have to find the
post) on how to create a child zone of an existing zone with DNSCMD.
Apparently from what the poster said, DNSCMD will just create a full FQDN of
the child and parent zone and not make it a child of the parent.

eg. dnscmd will create:
child1.domain.com
child2.domain.com
etc

instead of rather the expected (or desired):
domain.com
child1
child2
etc.

Not sure if you guys saw the post, but if you didn't, appreciate any
comments on it. I didn't respond since I wasn't sure how to get around that.

--
Regards,
Ace

Please direct all replies ONLY to the Microsoft public newsgroups
so all can benefit.

This posting is provided "AS-IS" with no warranties or guarantees
and confers no rights.

Ace Fekay, MCSE 2003 & 2000, MCSA 2003 & 2000, MCSE+I, MCT, MVP
Microsoft Windows MVP - Active Directory

HAM AND EGGS: A day's work for a chicken;
A lifetime commitment for a pig.
 
You know, that's interesting. I want to say dnscmd can't do that, but I'd be
lying if I say "I am sure" of that statement. I'd spend some time with
dnscmd and see if I can come up with a categorical statement.

--
Sincerely,

Dèjì Akómöláfé, MCSE MCSA MCP+I
Microsoft MVP - Directory Services
www.readymaids.com - COMPLETE SPAM Protection
www.akomolafe.com
Do you now realize that Today is the Tomorrow you were worried about
Yesterday? -anon


"Ace Fekay [MVP]"
 
dnscmd does not seem to have any facility for this. Very strange. Very
strange indeed. I'm going to post this to the other side and see what people
have to say about it.

--
Sincerely,

Dèjì Akómöláfé, MCSE MCSA MCP+I
Microsoft MVP - Directory Services
www.readymaids.com - COMPLETE SPAM Protection
www.akomolafe.com
Do you now realize that Today is the Tomorrow you were worried about
Yesterday? -anon


Deji Akomolafe said:
You know, that's interesting. I want to say dnscmd can't do that, but I'd be
lying if I say "I am sure" of that statement. I'd spend some time with
dnscmd and see if I can come up with a categorical statement.

--
Sincerely,

Dèjì Akómöláfé, MCSE MCSA MCP+I
Microsoft MVP - Directory Services
www.readymaids.com - COMPLETE SPAM Protection
www.akomolafe.com
Do you now realize that Today is the Tomorrow you were worried about
Yesterday? -anon


"Ace Fekay [MVP]"
In William Stacey [MVP] <[email protected]> asked for help and I offered
my suggestions below:
http://www.microsoft.com/technet/community/scriptcenter/network/default.mspx
William and Deji, there was a question asked last week (have to find the
post) on how to create a child zone of an existing zone with DNSCMD.
Apparently from what the poster said, DNSCMD will just create a full
FQDN
 
In
Deji Akomolafe said:
dnscmd does not seem to have any facility for this. Very strange. Very
strange indeed. I'm going to post this to the other side and see what
people have to say about it.

I tried to test it too as well and it couldn't do it. Here was the original
post concerning this:

From: <[email protected]>
Subject: Use dnscmd or script to add subdomain
Date: Tue, 6 Jul 2004 11:13:02 -0700


--
Regards,
Ace

Please direct all replies ONLY to the Microsoft public newsgroups
so all can benefit.

This posting is provided "AS-IS" with no warranties or guarantees
and confers no rights.

Ace Fekay, MCSE 2003 & 2000, MCSA 2003 & 2000, MCSE+I, MCT, MVP
Microsoft Windows MVP - Active Directory

HAM AND EGGS: A day's work for a chicken;
A lifetime commitment for a pig.
 
I am not sure if the OP was refering to a delegation or just a subdomain
node off the zone?
1) If Delegation - Just create the NS RR using dnscmd or gui.
2) If Node - DNS gui will allow creation of empty node, but will not save
when you reload the zone because their is no RR defined that would rebuild
the "empty" node. There is no RR that stands for empty node. You create
the subdomain indirectly by creating a RR in that subdomain, for example:
dnscmd /recordadd test.com. www.sub2.test.com. a "192.168.0.1"

Creates the www a record and an empty node (sub2) in the test.com domain.
The node is empty assuming no other rr in that path. If you delete that RR,
the node is deleted also (if last rr in the last rrset) as nothing exists to
"hold" that node.

So in summay, I think the answer he seeks (if I understand the question) is
to use /RecordAdd if using dnscmd. Please advise if I did not understand
the question. Cheers!
 
No problem. I had to figure this out myself recently using rfcs and other
reading and hair pulling. I finally figured out something that maid sense
to me and offer it here for others.
1) Remember www.sub1.test.com is a *Node in the db that also contains on
type A RRSet and one "A" RR (in this example).
2) A Node can contain zero or more RRSets. Empty leaf nodes are not stored
back into db as nothing to save. The GUI will add empty leaf nodes as GUI
sugar to allow easy add of RRs at that node, but node will disappear if no
records added.
3) A RRSet can contain RRs of the same type.
4) Only one RRSet per type can exist in a Node.
5) A RRSet can contain only one RR of that type and RData (i.e. can not
contain two TXT records with rdata "my text" or two IPs the same for A
records)

The logic to add a node/RR goes ~something like this:

bool return = AddRR( www.sub1.test.com., A, 30, "192.168.0.1")

bool AddRR(ownerName, Type, TTL, RData)
{
Node node = db.FindClosestNode(ownerName);
if ( node == null )
{
// node does not exist and was not a delegation, so add the Node,
RRSet, and RR,
// *including any/all empty Nodes *upto the zone name.
// Adding. www.subx.suby.subz.test.com. to test.com should add:
// subz.test.com. node, suby.subz.test.com. node,
subx.suby.subz.test.com. node and www.subx.suby.subz.test.com node.
}
else
{
if ( node.IsDelegation )
return false; // can not add rr to this db as Node is a
delegation.
// Node exists and was not delegation, so try to add rr to node.
RR rr = node.FindRR(Type, RData)
if ( rr != null )
return false; // RR exists.
node.AddRR(Type, TTL, RData);
return true;
}
}

It is a bit more involved then this, but that should give you an idea. For
me, it helps to understand the data structures and implementation a bit.
HTH

--
William Stacey, MVP

John Shaw said:
2) was the issue, and your response was exactly what I needed.

I work at an asp that hosts multiple software applications, one of these
uses SRV records. I have to create sub-domains with several SRV records for
multiple clients and could never figure it out.
Thanks for the Help.
 
In
William Stacey said:
No problem. I had to figure this out myself recently using rfcs and
other reading and hair pulling. I finally figured out something that
made sense to me and offer it here for others.

<snip>

William, that solution was awesome! Glad you were able to help John!

Ace
 
Thanks William Stacey! This is exactly what I needed.

As a side note, how did you figure out what API's to use for querying and deleting data? Where you able to piece it togethr from the various pages in msdn.microsoft.ocm or was it from another source?
 
hacking at WMITools and just staring at namespace until I saw what was going
on. Note the w2k3 and w2k have some different APIs signatures for adding
zones, etc. More are close or the same. Cheers!

--
William Stacey, MVP

m8v316 said:
Thanks William Stacey! This is exactly what I needed.

As a side note, how did you figure out what API's to use for querying and
deleting data? Where you able to piece it togethr from the various pages in
msdn.microsoft.ocm or was it from another source?
 
Now, that IS clever :) Thanks, William.

--
Sincerely,

Dèjì Akómöláfé, MCSE MCSA MCP+I
Microsoft MVP - Directory Services
www.readymaids.com - COMPLETE SPAM Protection
www.akomolafe.com
Do you now realize that Today is the Tomorrow you were worried about
Yesterday? -anon


William Stacey said:
I am not sure if the OP was refering to a delegation or just a subdomain
node off the zone?
1) If Delegation - Just create the NS RR using dnscmd or gui.
2) If Node - DNS gui will allow creation of empty node, but will not save
when you reload the zone because their is no RR defined that would rebuild
the "empty" node. There is no RR that stands for empty node. You create
the subdomain indirectly by creating a RR in that subdomain, for example:
dnscmd /recordadd test.com. www.sub2.test.com. a "192.168.0.1"

Creates the www a record and an empty node (sub2) in the test.com domain.
The node is empty assuming no other rr in that path. If you delete that RR,
the node is deleted also (if last rr in the last rrset) as nothing exists to
"hold" that node.

So in summay, I think the answer he seeks (if I understand the question) is
to use /RecordAdd if using dnscmd. Please advise if I did not understand
the question. Cheers!
 
Back
Top