Resource records are the basic unit of information in DNS. A name server's primary job is to respond to queries for resource records. Most people don't realize they are generating queries for resource records with nearly every network-based operation they do, including accessing a web site, pinging a host, or logging into Active Directory.
The WMI DNS Provider fully supports querying and manipulating resource records. Tables Table 14-5 and Table 14-6 list the supported properties and methods for the MicrosoftDNS_ResourceRecord class, which implements a generic interface for resource records.
The MicrosoftDNS_ResourceRecord class by itself is not enough. There are over two dozen types of resource records, and many have additional fields that don't have corresponding methods in the generic interface. To solve this problem, subclasses of MicrosoftDNS_ResourceRecord were created for each supported record type. Each subclass provides specific methods to access any field supported by the resource record type. Each supported resource record has a subclass with a name in the format of MicrosoftDNS_RRTypeType where RRType is the name of the record type, such as SRV, A, or PTR.
14.5.1 Finding Resource Records in a Zone
With the marriage of DNS and WMI, sending DNS queries has never been so easy. By using WQL, you can write complex query routines that would not have been possible previously. To list all of the resource records on a server, you simply need to execute the WQL query select * from MicrosoftDNS_ResourceRecord against the target server. The following example shows how to run the query against the local name server:
set objDNS = GetObject("winMgmts:root\MicrosoftDNS") set objRR = objDNS.ExecQuery("Select * from MicrosoftDNS_ResourceRecord") for Each objInst in objRR WScript.Echo objInst.TextRepresentation next
The TextRepresentation method is available to all resource record types since it's defined in MicrosoftDNS_ResourceRecord. It returns a text string representing the resource record, such as the following:
www.movie.edu. IN A 220.127.116.11
If you want to limit the query to only a specific zone, change the WQL query to include criteria for ContainerName, such as the following:
Select * from MicrosoftDNS_ResourceRecord Where ContainerName = 'ZoneName'
Since Active Directory stores all of the global catalog servers for a forest and domain controllers for a domain in DNS, you can write scripts to access this information and integrate it into your applications. The following example does exactly this by selecting all SRV records with a particular OwnerName. To find all global catalog servers in a forest, you can look up _ldap._tcp.gc._msdcs.ForestRootDNSName ; to find all domain controllers in a domain, look up _ldap._tcp.dc._msdcs.DomainDNSName.
option explicit Dim strDomain strDomain = "movie.edu" Dim objDNS, objRRs, objRR set objDNS = GetObject("winMgmts:root\MicrosoftDNS") set objRRs = objDNS.ExecQuery("Select * from MicrosoftDNS_SRVType " & _ " Where OwnerName = '_ldap._tcp.gc._msdcs." & _ strDomain & "'") WScript.Echo "Global Catalogs for " & strDomain for Each objRR in objRRs Wscript.Echo " " & objRR.DomainName next Wscript.Echo set objRRs = objDNS.ExecQuery("Select * from MicrosoftDNS_SRVType " & _ " Where OwnerName = '_ldap._tcp.dc._msdcs." & _ strDomain & "'") WScript.Echo "Domain Controllers for " & strDomain for Each objRR in objRRs Wscript.Echo " " & objRR.DomainName next
14.5.2 Creating Resource Records
With the DNS Provider, creating resource records can be done in a couple of steps. The CreateInstanceFromTextRepresentation method takes the following parameters: the domain name of the name server to create the record on, the domain name of the zone to add the record to, and the textual representation of the resource record. It also provides an out parameter that is a MicrosoftDNS_ResourceRecord object representing the newly created record.
The following example goes through the process of creating both an A and a PTR record. Both records are typically necessary when adding a new host to DNS:
option explicit Dim strRR, strReverseRR, strDomain, strReverseDomain ' A record to add strRR = "matrix.movie.edu. IN A 192.168.64.13" strDomain = "movie.edu" ' PTR record to add strReverseRR = "18.104.22.168.in-addr.arpa IN PTR matrix.movie.edu" strReverseDomain = "168.192.in-addr.arpa." Dim objDNS, objRR, objDNSServer, objRR2, objOutParam set objDNS = GetObject("winMgmts:root\MicrosoftDNS") set objRR = objDNS.Get("MicrosoftDNS_ResourceRecord") set objDNSServer = objDNS.Get("MicrosoftDNS_Server.Name="".""") ' Create the A record Dim strNull strNull = objRR.CreateInstanceFromTextRepresentation( _ objDNSServer.Name, _ strDomain, _ strRR, _ objOutParam) set objRR2 = objDNS.Get(objOutParam) WScript.Echo "Created Record: " & objRR2.TextRepresentation set objOutParam = Nothing ' Create the PTR record strNull = objRR.CreateInstanceFromTextRepresentation( _ objDNSServer.Name, _ strReverseDomain, _ strReverseRR, _ objOutParam) set objRR2 = objDNS.Get(objOutParam) WScript.Echo "Created Record: " & objRR2.TextRepresentation
The WMI DNS Provider fills a much-needed gap for programmatic management of a Microsoft DNS environment. In this chapter, we gave an overview of WMI and covered the classes used for managing name server and zone configuration along with the available properties and methods. We described how to query, add, and delete resource records with the DNS Provider and showed how you can get a list of Active Directory domain controllers using a simple WQL query.