' 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"

Dim objWMIService
Dim colItems
Dim objItem

' The script can do this on multiple computers, just add them to the array
arrComputers = Array("COMPUTERNAME")
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 replaces
				' the 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
