Our primary DNS server changed it's IP yesterday and we had a secondary DNS server with several domains that needed to have the IP for the master server updated. Windows' gift and curse is that you don't have a bunch of text files where you can find everything but manually opening the properties of every domain, removing the previous master IP, adding a new IP and moving on to the next domain was just not an option.
Now, the main event: a script to update the master server IP for all secondary DNS zones on a Windows computer.
-
' This script updates the Master server IP for all secondary zones on the
-
' specifed computers.
-
-
' Constants for WMI
-
Const wbemFlagReturnWhenComplete = &H0
-
Const wbemFlagReturnImmediately = &h10
-
Const wbemFlagForwardOnly = &h20
-
Const iDnsSecondary = 2
-
-
-
' The array to hold the master servers
-
' (there can be multiple master servers)
-
Dim MasterServersArray(0)
-
' Add the master servers to the array. Replace with your own IP
-
MasterServersArray(0) = "192.168.2.10"
-
-
' The address of the old master server, replace with your own IP
-
' Used to check the zone and only update those that need it.
-
Dim strOldMasterServer
-
strOldMasterServer = "10.0.0.10"
-
-
' The script can do this on multiple computers,
-
' just add them to the array
-
Dim arrComputers
-
arrComputers = Array("COMPUTERNAME")
-
-
Dim objWMIService
- Dim colItems
-
Dim objItem
-
-
For Each strComputer In arrComputers
-
WScript.Echo
-
WScript.Echo "=========================================="
-
WScript.Echo "Computer: " & strComputer
-
WScript.Echo "=========================================="
-
-
WScript.Echo "Fetching zones..."
-
' Connect to the DNS WMI interface on the current computer
-
Set objWMIService = GetObject("winmgmts:\\" & strComputer _
-
& "\root\MicrosoftDNS")
-
-
' Get all zones from the DNS server. We use wbemFlagReturnWhenComplete
-
' to make sure the call completes before returning. If not we often get
-
' errors when looping through the returned collection. If you only want
-
' some zones to be updated you can filter your query just like any SQL query.
-
Set colItems = objWMIService.ExecQuery("SELECT * FROM MicrosoftDNS_Zone", _
-
"WQL", wbemFlagReturnWhenComplete + wbemFlagForwardOnly)
-
WScript.Echo "Retrieved zones."
-
-
' Used to hold a return object.
-
dim objTmp
-
-
For Each objItem In colItems
-
' Only secondary zones have master servers
-
if objItem.ZoneType = iDnsSecondary then
-
' Get the current master IPs
-
strMastersIPAddressesArray = Join(objItem.MastersIPAddressesArray, ",")
-
-
' Some information
-
WScript.Echo "Name: " & objItem.Name
-
WScript.Echo "MastersIPAddressesArray: " & strMastersIPAddressesArray
-
-
' We'll only do this in the zones with the old master server
-
' No need to update zones that don't need it.
-
if strMastersIPAddressesArray = strOldMasterServer then
-
' ResetMasterIpArray is a function on the WMI object
-
' that replacesthe current array of Master IPs.
-
' objTemp receives a refrence to the updated zone.
-
objItem.ResetMasterIpArray MasterServersArray, objTemp
-
' We don't need it so we set it to nothing
-
' immediatly to save memory
-
set objTemp = nothing
-
WScript.Echo
-
end If
-
' Let's just make the secondary zone refresh from
-
' the master server while we're at it
-
objItem.ForceRefreshOfSecondaryZoneFromMaster()
-
end if
-
Next
-
Next