Recreate DNS Hosts From Export File Into a Microsoft DNS Server

Recreate DNS Hosts From Export File Into a Microsoft DNS Server

The following script was created to add hosts, from a list of exported hosts and IPs originally from a unrelated DNS server’s zone, into the specified Microsoft DNS server’s DNS zone.

The basic usage of the script is:

CScript AddDNSHost.vbs /DNSServer:DNSServer /DNSZone:DNSZone /HostName:HostName /HostIP:HostIP

And an example:

AddDNSHost.vbs /DNSServer:192.168.0.1 /DNSZone:DNSZone.local /HostName:MyComputer /HostIP:192.168.1.123

A very simple way of constructing multiple commands can be achieved with practically any speadsheet application where column A holds the list of host names, column B holds their respected IP addresses and column C the following command (starting from row 1):

="AddDNSHost.vbs /DNSServer:192.168.0.1 /DNSZone:DNSZone.local /HostName:" & A1 & " /HostIP:" & B1

The above command can be copied once for each row.

The resulting constructed command can then be directly pasted into a command prompt

'* * * * * * * * * * * Start of AddDNSHost.vbs * * * * * * * * * * *

On Error Resume Next

strDNSServer = Wscript.Arguments.Named("DNSServer")
strDNSZone = Wscript.Arguments.Named("DNSZone")
strHostName = Wscript.Arguments.Named("HostName")
strHostIP = Wscript.Arguments.Named("HostIP")

If Len(Trim(strDNSServer)) > 0 And Len(Trim(strDNSZone)) > 0 And Len(Trim(strHostName)) > 0 And Len(Trim(strHostIP)) > 0 Then

    If Right(UCase(strHostName), Len(strDNSZone) + 1) <> "." & UCase(strDNSZone) Then
        strHostName = strHostName & "." & strDNSZone
    End If

    intRecordClass = 1
    intTTL = 600
    strComputer = "."

    Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\MicrosoftDNS")
    Set objItem = objWMIService.Get("MicrosoftDNS_AType")
    intReturn = objItem.CreateInstanceFromPropertyData(strDNSServer, strDNSZone, strHostName, intRecordClass, intTTL, strHostIP)

    If Err.Number = 0 And intReturn = 0 Then
        WScript.Echo strHostName & vbTab & "Added"
    Else
        WScript.Echo strHostName & vbTab & "Failed"
    End If

End If

'* * * * * * * * * * * End of AddDNSHost.vbs * * * * * * * * * * *

Leave a Reply