| [ Team LiB ] |
|
27.2 Manipulating DNS Server ConfigurationThere are close to 50 different settings that can be configured on a Microsoft DNS server. They range from default scavenging and logging settings to settings that customize the DNS server behavior, such as how zone transfers will be sent to secondaries and whether to round-robin multiple A record responses. The DNS provider is mapped to the root\MicrosoftDNS namespace. A DNS server is represented by an instance of a MicrosoftDNS_Server class, which is derived from the CIM_Service class. Table 27-1 contains all the property methods available in the MicrosoftDNS_Server class.
The MicrosoftDNS_Server class also provides a few methods to initiate certain actions on the DNS server. Perhaps two of the most useful are StartService and StopService, which allow you to start and stop the DNS service. Table 27-2 contains the list of methods available to the MicrosoftDNS_Server class
27.2.1 Listing a DNS Server's PropertiesThe first step in programmatically managing your DNS server configuration is to see what settings you currently have and determine whether any need to be modified. With WMI, it is really easy to list all properties for the server. The following example shows how to do it: Set objDNS = GetObject("winMgmts:root\MicrosoftDNS")
set objDNSServer = objDNS.Get("MicrosoftDNS_Server.Name="".""")
Wscript.Echo objDNSServer.Properties_.Item("Name") & ":"
for each objProp in objDNSServer.Properties_
if IsNull(objProp.Value) then
Wscript.Echo " " & objProp.Name & " : NULL"
else
if objProp.IsArray = TRUE then
For I = LBound(objProp.Value) to UBound(objProp.Value)
wscript.echo " " & objProp.Name & " : " & objProp.Value(I)
next
else
wscript.echo " " & objProp.Name & " : " & objProp.Value
end if
end if
next
After getting a WMI object for the DNS provider (root\MicrosoftDNS), we get a MicrosoftDNS_Server object by looking for the "." instance. Since there can only be one instance of MicrosoftDNS_Server running on any given computer, we do not need to worry about multiple objects. After getting a MicrosoftDNS_Server object, we iterate through all the properties of the object and print each one out. Note that we have added special checks for values that contain arrays to print each element of the array. In that case, we use Lbound and Ubound to iterate over all the values for the array. 27.2.2 Configuring a DNS serverNow that we can see what values have been set on our DNS server, we may want to change some of them. To do so is very straightforward. We simply need to set the property method (e.g., EventLogLevel) to the correct value. This example shows how it can be done: on error resume next
Set objDNS = GetObject("winMgmts:root\MicrosoftDNS")
set objDNSServer = objDNS.Get("MicrosoftDNS_Server.Name="".""")
Wscript.Echo objDNSServer.Name & ":"
objDNSServer.EventLogLevel = 4
objDNSServer.LooseWildcarding = True
objDNSServer.MaxCacheTTL = 900
objDNSServer.MaxNegativeCacheTTL = 60
objDNSServer.AllowUpdate = 3
objDNSServer.Put_
if Err then
Wscript.Echo " Error occurred: " & Err.Description
else
WScript.Echo " Change successful"
end if
Note that we had to call Put_ at the end. If we didn't, none of the changes would have been committed. 27.2.3 Restarting the DNS ServiceAfter making changes to DNS settings, you typically will need to restart the DNS service for them to take effect. We can utilize the StopService and StartService methods as shown in the following example to do this: on error resume next
Set objDNS = GetObject("winMgmts:root\MicrosoftDNS")
set objDNSServer = objDNS.Get("MicrosoftDNS_Server.Name="".""")
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 "Restart successful"
27.2.4 DNS Server Configuration Check ScriptBuilding on the examples we've used so far in this chapter, we can now move forward with writing a robust DNS server configuration check script. A configuration check script can be very important, especially in large environments where you may have many DNS servers. Unless you have a script that routinely checks the configuration on all of your DNS servers, it is very likely that those servers will not have an identical configuration. If this is true, when problems pop up over time, you may end up spending considerably more time troubleshooting because of the discrepancies between the servers. To accomplish the configuration checking, we will store each setting in a VBScript Dictionary object. For those coming from other languages such as Perl, a Dictionary object is the VBScript analog of a hash or associative array. It is not extremely flexible but works well in situations such as what we need. Another option would be to store the settings in a text file and read them into a Dictionary object when the script starts up. Example 27-1 contains the configuration check code. Example 27-1. DNS Server configuration check scriptoption explicit
on error resume next
Dim arrServers
Dim strUsername, strPassword
Dim dicDNSConfig
` Array of DNS servers to check
arrServers = Array("dns1.mycorp.com","dns2.mycorp.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 the other three examples shown so far in this chapter. We added a server array so that you can check multiple servers at once. Then for each server, the script simply checks each key in the Dictionary object to see whether the value for it matches that on the DNS 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 service if a change has been made to its configuration. If a change has not been made, it proceeds to the next server. One enhancement that would make the process even more automated would be to dynamically query the list of DNS servers instead of hardcoding them in an array. You simply would need to query the NS record for one or more zones that your DNS servers are authoritative for. As long as an NS record is added for each new name server, the script would automatically pick it up in subsequent runs. Later in the chapter, we will show how to query DNS with the DNS provider. |
| [ Team LiB ] |
|