Saturday, June 18, 2011

How to speed up Queries to MicrosoftDNS with WMI

Repost from old blog

So there are many key things to remember when creating your WMI queries make them as specific as you can.

For example if you have ~5000 zones on your Microsoft DNS server and you are looking to see if a single record exists in one of those zones the wrong query could take 1 min+ to complete.


If you do a query like Select * from MicrosoftDNS_AType where ownername="" it is going to take a while. Because you did not specify where to look for this record it is going to look in the RootHints and in the DNS Cache also. So if you have a public DNS server that does recursive lookups it could have a few hundred thousand extra records.

So a better query would be Select TextRepresentation from MicrosoftDNS_AType where containername="mydomainname" and domainname="" and

You can use a vbscript like the one below to test your queries. This will show you the correct domainname and other settings to use in your query. You can remove the where clause to show all the data on the server.

The containername will be the name of the zone that holds the records that you want to query for (Ie If you leave the containername empty it will also search through the DNS cache.

The domainname specifies the child folder (don’t know how else to describe it). So if you have the domainname is

Now the domainname will change depending on if there are sub domains to the subdomain (ie www from example above). So if www does not exist then the domainname is And of course there are exceptions to this rule. If there ever was a child to the sub (you deleted www but left then the domainname is If you don’t want to attempt to do the logic around making sure you have the correct domainname you can omit it. But if you have a large number of records it could make it slow.

Like SQL the order of the statements in the query is also important.

If you know the full record info (hostname, TYPE, data) it is fastest to generate the text representation and query on that. You can do that by adding changing your query to something like:

Select * from MicrosoftDNS_AType where containername=”” and domainname=”” and TextRepresentation=" IN A"

on error resume next
servername = "."
domainname = ""

recordtype = "A"

set dnsserver = Getobject("winmgmts:{Authenticationlevel=pktPrivacy}!\\" & servername & "\root\MicrosoftDNS")

query = "Select * from MicrosoftDNS_" & recordtype & "Type where containername=""" & domainname & """"
wscript.echo "Query=" & query

Set colItems = dnsserver.ExecQuery(query,,48)
if colitems.count <> 0 then
For Each objItem in colItems
Wscript.Echo "ContainerName: " & objItem.ContainerName
Wscript.Echo "DnsServerName: " & objItem.DnsServerName
Wscript.Echo "DomainName: " & objItem.DomainName
Wscript.Echo "OwnerName: " & objItem.OwnerName
Wscript.Echo "PrimaryName: " & objItem.PrimaryName
Wscript.Echo "RecordClass: " & objItem.RecordClass
Wscript.Echo "RecordData: " & objItem.RecordData
Wscript.Echo "TextRepresentation: " & objItem.TextRepresentation
Wscript.Echo "Timestamp: " & objItem.Timestamp
Wscript.Echo "TTL: " & objItem.TTL
end if

1 comment:

Fred Castillo said...

I never realized specificity had such an impact on the speed of WMI queries... fascinating stuff! I'll definitely have to try this for myself when I have some extra time.

Fred |