Recipe13.11.Creating and Deleting Resource Records


Recipe 13.11. Creating and Deleting Resource Records

Problem

You want to create or delete resource records.

Solution

Using a graphical user interface

  1. Open the DNS snap-in (dnsmgmt.msc).

  2. Connect to the DNS Server you want to modify. In the left pane, right-click on DNS and select Connect to DNS Server. Select The following computer and enter the target server name. Click OK.

  3. If you want to add or delete a record in a forward zone, expand the Forward Lookup Zone folder. If you want to add or delete a record for a reverse zone, expand the Reverse Lookup Zone folder.

To create a resource record, do the following:

  1. In the left pane, right-click the zone and select the option that corresponds to the record type you want to createe.g., New Host (A).

  2. Fill in all required fields.

  3. Click OK.

To delete a resource record, do the following:

  1. In the left pane, click on the zone where the record is.

  2. In the right pane, right-click on the record you want to delete and select Delete.

  3. Click Yes to confirm.

Using a command-line interface

To add a resource record, use the following command:

> dnscmd <ServerName> /recordadd <ZoneName> <NodeName> <RecordType> <RRData>

The following example adds an A record in the rallencorp.com zone:

> dnscmd dc1 /recordadd rallencorp.com myhost01 A 19.25.52.25

To delete a resource record, use the following command:

> dnscmd <ServerName> /recorddelete <ZoneName> <NodeName> <RecordType> <RRData>

The following example deletes an A record in the rallencorp.com zone:

> dnscmd dns01 /recorddelete rallencorp.com myhost01 A 19.25.52.25

Using VBScript
' This code shows how to add an A record and PTR record using ' the DNS WMI Provider.     ' This code must be executed directly on a DNS Server.     ' ------ SCRIPT CONFIGURATION ------ strForwardRRAdd = "myhost01.rallencorp.com. IN A 19.25.52.25" strReverseRRAdd = "25.52.25.19.in-addr.arpa IN PTR myhost01.rallencorp.com" strForwardDomain = "rallencorp.com" strReverseDomain = "25.19.in-addr.arpa." ' ------ END CONFIGURATION ---------     set objDNS = GetObject("winMgmts:root\MicrosoftDNS") set objRR = objDNS.Get("MicrosoftDNS_ResourceRecord") set objDNSServer = objDNS.Get("MicrosoftDNS_Server.Name="".""")     ' Create the A record strNull = objRR.CreateInstanceFromTextRepresentation( _                   objDNSServer.Name, _                   strForwardDomain, _                   strForwardRRAdd, _                   objOutParam)        set objRR2 = objDNS.Get(objOutParam) WScript.Echo "Created Record: " & objRR2.TextRepresentation     ' Create the PTR record strNull = objRR.CreateInstanceFromTextRepresentation( _                   objDNSServer.Name, _                   strReverseDomain, _                   strReverseRRAdd, _                   objOutParam)        set objRR2 = objDNS.Get(objOutParam) WScript.Echo "Created Record: " & objRR2.TextRepresentation ' This code shows how to delete an A and PTR record for the record ' I created in the previous example.     strHostName  = "myhost01.rallencorp.com."     set objDNS = GetObject("winMgmts:root\MicrosoftDNS") set objDNSServer = objDNS.Get("MicrosoftDNS_Server.Name="".""")     set objRRs = objDNS.ExecQuery(" select * " & _                           " from MicrosoftDNS_ResourceRecord " & _                           " where OwnerName = """ & strHostName & """" & _                           " Or RecordData = """ & strHostName & """") if objRRs.Count < 1 then    WScript.Echo "No matches found for " & strHostName else    for each objRR in objRRs       objRR.Delete_       WScript.Echo "Deleted " & objRR.TextRepresentation    next end if

Discussion

Using a graphical user interface

The DNS snap-in is good for creating a small number of records, but if you need to add or delete more than a couple of dozen, I recommend writing a batch file around dnscmd or preferably the DNS WMI Provider.

Using a command-line interface

Adding A, CNAME, and PTR resource records is pretty straightforward as far as the data you must enter, but other record types, such as SRV, require quite a bit more data. Run dnscmd /recordadd /? and dnscmd /recorddelete /? for the required parameters for each record type.

Using VBScript

In the first example, I created A and PTR records using the CreateIn-stanceFromTextRepresentation method, which is a MicrosoftDNS_ResourceRecord method that allows you to create resource records by passing in the textual version of the record. This is the textual representation of the A record used in the example:

myhost01.rallencorp.com IN A 19.25.52.25

The first parameter to this method is the DNS Server name, the second is the name of the domain to add the record to, the third is the resource record, and the last is an out parameter that returns a reference to the new resource record.

In the second example, I find all resource records that match a certain hostname and delete them. This is done by first using a WQL query to find all resource records where the OwnerName equals the target host name (this will match any A records) and where RecordData equals the target host name (this will match any PTR records). The Delete_ method is called on each matching record, removing them on the DNS Server.

See Also

MSDN: MicrosoftDNS_ResourceRecord



Windows Server Cookbook
Windows Server Cookbook for Windows Server 2003 and Windows 2000
ISBN: 0596006330
EAN: 2147483647
Year: 2006
Pages: 380
Authors: Robbie Allen

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net