Recipe 13.19. Script: DNS Server Configuration CheckerConfiguring a large number of DNS Servers can be a chore. And unless you have a script that routinely checks the configuration on all of your DNS Servers, it's very likely that over time those servers will not have identical configurations. One administrator may make a change on one server and not another. If the servers don't have identical configurations, when problems pop up you may end up spending a lot of time troubleshooting false negatives because of the discrepancies. Using the WMI DNS Provider, we can write a script that checks the configuration of a number of servers and updates them as necessary. To perform the configuration checking, we'll store each setting in a VBScript Dictionary object. Another option would be to store the settings in a text file and read them into a Dictionary object when the script starts up. The script iterates over a list of servers, checks the settings on each server, and modifies settings as necessary. Here is the script's code: option explicit on error resume next Dim arrServers Dim strUsername, strPassword Dim dicDNSConfig ' Array of DNS Servers to check arrServers = Array("dns01.rallencorp.com","dns02.rallencorp.com") ' User and password that can modify the config on the DNS Servers strUsername = "dnsadmin" strPassword = "dnspwd" ' This dictionary object will contain the key value pairs for all ' the settings that you want to check and configure on the DNS Servers. Set dicDNSConfig = CreateObject("Scripting.Dictionary") dicDNSConfig.Add "AllowUpdate", 1 dicDNSConfig.Add "LooseWildCarding", TRUE dicDNSConfig.Add "MaxCacheTTL", 900 dicDNSConfig.Add "MaxNegativeCacheTTL", 60 dicDNSConfig.Add "EventLogLevel", 0 dicDNSConfig.Add "StrictFileParsing", TRUE dicDNSConfig.Add "DisableAutoReverseZones", TRUE Dim arrDNSConfigKeys arrDNSConfigKeys = dicDNSConfig.keys Dim objLocator Set objLocator = CreateObject("WbemScripting.SWbemLocator") Dim x, y, boolRestart For x = LBound(arrServers) to UBound(arrServers) boolRestart = False WScript.echo arrServers(x) Dim objDNS, objDNSServer Set objDNS = objLocator.ConnectServer(arrServers(x), _ "root\MicrosoftDNS", _ strUserName, strPassword) set objDNSServer = objDNS.Get("MicrosoftDNS_Server.Name="".""") for y = 0 To dicDNSConfig.Count - 1 Dim strKey strKey = arrDNSConfigKeys(y) WScript.Echo " Checking " & strKey if dicDNSConfig.Item(strKey) <> objDNSServer.Properties_.Item(strKey) then objDNSServer.Properties_.Item(strKey).value = dicDNSConfig(strKey) objDNSServer.Put_ boolRestart = TRUE if Err Then WScript.Echo " Error setting " & strKey & " : " & _ Err.Description Wscript.Quit else WScript.Echo " " & strKey & " updated" end if end if Next if boolRestart then objDNSServer.StopService if Err Then WScript.Echo "StopService failed: " & Err.Description Wscript.Quit end if objDNSServer.StartService if Err Then WScript.Echo "StartService failed: " & Err.Description Wscript.Quit end if WScript.Echo "Restarted" end if WScript.Echo "" next Besides the use of the Dictionary object, most of the script is a combination of several recipes in this chapter. I added a server array so that you can check multiple servers at once. For each server, the script simply checks each key in the Dictionary object to see if its value matches the key on the name server. If not, it modifies the server and commits the change via Put_. After it's done looping through all the settings, it restarts the DNS Server service if a change has been made to its configuration. It then proceeds to the next server. One enhancement to automate the process even more would be to dynamically query the list of name servers instead of hardcoding them in an array. You would need to look up the NS records for one or more zones for which your name servers are authoritative. As long as an NS record is added for each new name server, the script would automatically discover new name servers on subsequent runs (kind of like a stub zone). |