[ Team LiB ] |
27.4 Creating and Manipulating Resource RecordsResource records are the basic unit of information in DNS. A DNS server's primary job is to respond to queries for resource records. Most people don't realize they are generating resource record queries with nearly every network-based operation they do, including accessing a website, pinging a host, or logging into Active Directory. Resource records come in many different flavors or types. Each type corresponds to a certain type of name or address lookup. Each record type also has additional information encoded with the record that represents things such as the time to live of the record. The following is a textual example of what a CNAME record looks like: www.mycorp.com. 1800 IN CNAME www1.mycorp.com. Or more generically: Owner TTL Class Type RR-Data Now let's break the record down into its individual parts:
The WMI DNS provider fully supports querying and manipulating resource records. In Table 27-5 and Table 27-6, the supported properties and methods are listed 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 different types of resource records with many having additional fields that would not 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_<RR Type>Type. To show just how different resource records can be, let's take a look at an A record: www.mycorp.com. 1800 IN A 192.10.4.5 Now let's compare that with an SRV record: _ldap._tcp.dc._msdcs.mycorp.com 1800 IN SRV 0 100 389 dc1.mycorp.com. As you can see, the SRV record has several additional fields. By using the MicrosoftDNS_SRVType subclass, we can access each field with methods provided by the class. The complete list of supported resource record types is provided in Table 27-7.
27.4.1 Finding Resource Records in a ZoneWith the marriage of DNS and WMI, querying DNS 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 what this would look like if the script is run on a DNS 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 is defined in MicrosoftDNS_ResourceRecord. It will return a text string representing the resource record, such as the following: www.mycorp.com. IN A 192.10.4.5 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 uses DNS to store all of the Global Catalog servers in a forest and domain controllers in a domain, you can write scripts to utilize DNS 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 simply query _ldap._tcp.gc._msdcs.<ForestDNSName>, and to find all domain controllers in a domain, query _ldap._tcp.dc._msdcs.<DomainDNSName>. option explicit Dim strDomain strDomain = "mycorp.com" 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 27.4.2 Creating Resource RecordsWith the DNS provider, creating resource records is also very easy to do. The MicrosoftDNS_ResourceRecord::CreateInstanceFromTextRepresentation method takes the server name to create the record on, the domain name, and the text representation of the resource record as in parameters. It also provides an out parameter which will be an object representing the newly created record. Example 27-3 goes through the process of creating both A and PTR records. Both records are typically necessary when adding a new host to DNS. Example 27-3. Creating A and PTR resource recordsoption explicit Dim strRR, strReverseRR, strDomain, strReverseDomain ' A record to add strRR = "testb.mycorp.com. IN A 192.32.64.13" strDomain = "mycorp.com" ' PTR record to add strReverseRR = "13.64.32.192.in-addr.arpa IN PTR testb.mycorp.com" strReverseDomain = "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 |
[ Team LiB ] |