My digital notepad RSS 2.0
 Thursday, January 11, 2007

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.

That's why we have Windows Management Instrumentation (WMI). For an administrator it's the best thing that's happened since they started slicing bread. Well, not quite, but if you intend to do some serious administration of Windows servers you'd better look into it. If you don't like looking through a bunch of documentation for figure out what to do, use Scriptomatic 2.0 like I did yesterday. It will generate a basic script for you that can easily be modified.

Now, the main event: a script to update the master server IP for all secondary DNS zones on a Windows computer.

  1. ' This script updates the Master server IP for all secondary zones on the
  2. ' specifed computers.
  3.  
  4. ' Constants for WMI
  5. Const wbemFlagReturnWhenComplete = &H0
  6. Const wbemFlagReturnImmediately = &h10
  7. Const wbemFlagForwardOnly = &h20
  8. Const iDnsSecondary = 2
  9.  
  10.  
  11. ' The array to hold the master servers
  12. ' (there can be multiple master servers)
  13. Dim MasterServersArray(0)
  14. ' Add the master servers to the array. Replace with your own IP
  15. MasterServersArray(0) = "192.168.2.10"
  16.  
  17. ' The address of the old master server, replace with your own IP
  18. ' Used to check the zone and only update those that need it.
  19. Dim strOldMasterServer
  20. strOldMasterServer = "10.0.0.10"
  21. ' The script can do this on multiple computers,
  22. ' just add them to the array
  23. Dim arrComputers
  24. arrComputers = Array("COMPUTERNAME")
  25.  
  26. Dim objWMIService
  27. Dim colItems
  28. Dim objItem
  29.  
  30. For Each strComputer In arrComputers
  31.         WScript.Echo
  32.         WScript.Echo "=========================================="
  33.         WScript.Echo "Computer: " & strComputer
  34.         WScript.Echo "=========================================="
  35.        
  36.         WScript.Echo "Fetching zones..."
  37.         ' Connect to the DNS WMI interface on the current computer
  38.         Set objWMIService = GetObject("winmgmts:\\" & strComputer _
  39.                 & "\root\MicrosoftDNS")
  40.  
  41.         ' Get all zones from the DNS server. We use wbemFlagReturnWhenComplete
  42.         ' to make sure the call completes before returning. If not we often get
  43.         ' errors when looping through the returned collection. If you only want 
  44.         ' some zones to be updated you can filter your query just like any SQL query.
  45.         Set colItems = objWMIService.ExecQuery("SELECT * FROM MicrosoftDNS_Zone", _
  46.                 "WQL", wbemFlagReturnWhenComplete + wbemFlagForwardOnly)
  47.         WScript.Echo "Retrieved zones."
  48.  
  49.         ' Used to hold a return object.
  50.         dim objTmp
  51.  
  52.         For Each objItem In colItems
  53.           ' Only secondary zones have master servers
  54.           if objItem.ZoneType = iDnsSecondary then     
  55.                 ' Get the current master IPs 
  56.                         strMastersIPAddressesArray = Join(objItem.MastersIPAddressesArray, ",")
  57.                        
  58.                         ' Some information
  59.                         WScript.Echo "Name: " & objItem.Name
  60.                         WScript.Echo "MastersIPAddressesArray: " & strMastersIPAddressesArray
  61.  
  62.                         ' We'll only do this in the zones with the old master server
  63.                         ' No need to update zones that don't need it.
  64.                         if strMastersIPAddressesArray = strOldMasterServer then
  65.                           ' ResetMasterIpArray is a function on the WMI object
  66.                           ' that replacesthe current array of Master IPs.
  67.                           ' objTemp receives a refrence to the updated zone.
  68.                           objItem.ResetMasterIpArray MasterServersArray, objTemp
  69.                           ' We don't need it so we set it to nothing
  70.                           ' immediatly to save memory
  71.                           set objTemp = nothing
  72.                           WScript.Echo
  73.                         end If
  74.                         ' Let's just make the secondary zone refresh from
  75.                         ' the master server while we're at it
  76.                         objItem.ForceRefreshOfSecondaryZoneFromMaster()
  77.           end if
  78.         Next
  79. Next
UpdateMasterServerIP.vbs (2,82 KB)
 
Thursday, January 11, 2007 9:39:17 AM (Central Europe Standard Time, UTC+01:00)  #    Comments [0] -
Administration | Scripting
Links
Twitter updates
    Archive
    <January 2007>
    SunMonTueWedThuFriSat
    31123456
    78910111213
    14151617181920
    21222324252627
    28293031123
    45678910
    About the author/Disclaimer

    Disclaimer
    The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

    © Copyright 2012
    Glenn F. Henriksen
    Sign In
    Statistics
    Total Posts: 46
    This Year: 0
    This Month: 0
    This Week: 0
    Comments: 31
    All Content © 2012, Glenn F. Henriksen
    DasBlog theme 'Business' created by Christoph De Baene (delarou)