13.7.1 ProblemYou want to create and delete resource records. 13.7.2 Solution13.7.2.1 Using a graphical user interface
13.7.2.2 Using a command-line interfaceTo add a resource record, use the following command: > dnscmd <DNSServerName> /recordadd <ZoneName> <NodeName> <RecordType> <RRData> The following command adds an A record in the rallencorp.com zone: > dnscmd dc1 /recordadd rallencorp.com wins01 A 19.25.52.2.25 To delete a resource record, use the following command: > dnscmd <DNSServerName> /recorddelete <ZoneName> <NodeName> <RecordType> <RRData> The following command deletes an A record in the rallencorp.com zone: > dnscmd dc1 /recorddelete rallencorp.com wins01 A 19.25.52.2.25 13.7.2.3 Using VBScript' This code shows how to add an A record and PTR record using ' the DNS WMI Provider ' ------ SCRIPT CONFIGURATION ------ strForwardRRAdd = "test-xp.rallencorp.com. IN A 192.32.64.13" strReverseRRAdd = "13.64.32.192.in-addr.arpa IN PTR test-xp.rallencorp.com" strForwardDomain = "rallencorp.com" strReverseDomain = "192.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 = "test-xp.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 13.7.3 Discussion13.7.3.1 Using a graphical user interfaceThe DNS Management 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, then I'd recommend writing a batch file around dnscmd or preferably, use the DNS WMI Provider. 13.7.3.2 Using a command-line interfaceAdding 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. The help pages for /recordadd and /recorddelete display the required information for each record type. 13.7.3.3 Using VBScriptIn the first example, I created A and PTR records using the CreateInstanceFrom TextRepresentation 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: test-xp.rallencorp.com IN A 192.32.64.13 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. 13.7.4 See AlsoMSDN: MicrosoftDNS_ResourceRecord |