One of the biggest complaints I have with Microsoft DNS is it's Manager. Once you get several thousand DNS entries it can take a long time for the manager to load. It's even more frustrating when you're called in the middle of the night and trying to load DNS Manager over a VPN connection.
Once the DNS Manager snap-in is loaded, the ability to search or filter is non-existent.
It seems that over 90% of the requests I receive regarding DNS is creating and changing A and CAME records. A simple task that should only take a few seconds, but always seems to take forever. So, I started to look at alternative ways to manage A and CNAME records.
DNSCMD would allow me to create and modify the records, but have you seen the syntax for dnscmd lately? Of course it could be wrapped in a batch file, but if you ever wanted to migrate to code into Info Path or a web application, it's not going to work very nice.
When every I need a system type script written, the first place I look is WMI. Sure enough, DNS is complete exposed via WMI on the DNS Server. The namespace is root\MicrosoftDNS.
The class MicrosoftDNS_Resource Record has a method of CreateInstanceFromTextRepresentation that will create or A and CNAME records.
CreateInstanceFromTextRepresentation requires 4 parameters: DNSServerName, ContainerName, TextRepresentation, MicrosoftDNS_ResourceRecordRR.
DNSServerName is pretty simple. That's a string with the FQDN of the DNS Server you are creating the record on. (e.g. dns01.kdhtechnology.com)
ContainerName is a string that contains the domain where you want the record created. (e.g. kdhtechnology.com)
TextRepresentation is a little a string that contains the record definition. (e.g. foo.kdhtechnology.com IN A 192.168.1.25 )
MicrosoftDNS_ResourceRecordRR is the return value from the function and it contains the newly created Resource Record.
So, here is the code to create an A record.
strServer = “dns01.kdhtechnology.com”
strName = “foo”
strDomain = “kdhtechnology.com”
strIP = “192.168.1.25”
Set refWMI = GetObject(":\\" & strServer & "\root\MicrosoftDNS")
Set objRR = refWMI.Get("MicrosoftDNS_ResourceRecord")
Set objMethod = objRR.Methods_("CreateInstanceFromTextRepresentation")
Set objInParm = objMethod.inParameters.SpawnInstance_()
objInParm.TextRepresentation = strName & "." & strDomain & " IN A " & strIP
objInParm.DnsServerName = strServer & "." & strDomain
objInParm.Containername = strDomain
set objOutParm = objRR.ExecMethod_("CreateInstanceFromTextRepresentation", objInParm)
Set objRR = refWMI.Get(objOutParm.RR)
For those who do not want to type in all the code, I have several VBS scripts in the download area. WMI-DNS.zip
The .VBS files also include the code to create reverse lookups and checks to see if the record exists and gives your the opportunity to delete the record first.
Kurt