| [ Team LiB ] |
|
27.3 Creating and Manipulating ZonesThe MicrosoftDNS_Zone class provides a plethora of properties and methods to aid in managing your zones. Even if you are using AD-integrated zones, which help reduce the amount of work it takes to maintain DNS, you will inevitably need to configure settings on a zone or create additional zones. In Table 27-3 and Table 27-4, the list of available properties and methods for the MicrosoftDNS_Zone class are presented.
27.3.1 Creating a ZoneCreating a zone with the DNS provider is a straightforward operation. You simply need to get a WMI object for the DNS namespace, instantiate an object from the MicrosoftDNS_Zone class, and call CreateZone on that object. The next example shows how to do this: on error resume next
strNewZone = "mycorp.com."
Set objDNS = GetObject("winMgmts:root\MicrosoftDNS")
Set objDNSZone = objDNS.Get("MicrosoftDNS_Zone")
strNull = objDNSZone.CreateZone(strNewZone,0,True)
if Err then
WScript.Echo "Error occurred creating zone: " & Err.Description
else
WScript.Echo "Zone created..."
end if
The three parameters we passed into CreateZone( ) include the zone name, zone type flag, and DS-Integrated flag. A zone type of 0 means to create a primary zone. When the DS-Integrated flag is set to true, the primary zone will be AD-integrated; if it is false, it will be a standard primary. At the time of this writing, Microsoft had conflicting documentation about these parameters and their valid values. Refer to the MSDN Library for more information; hopefully they will get it right eventually. 27.3.2 Configuring a ZoneConfiguring a zone is not too different from configuring a server. The primary difference is how you instantiate a MicrosoftDNS_Zone object. To use the Get( ) method on a WMI (SWbemServices) object, you have to specify the keys for the class you want to instantiate. For the MicrosoftDNS_Zone class, the keys include ContainerName, DnsServerName, and Name. In this case, ContainerName and Name are the name of the zone. The DnsServerName we retrieve by getting a MicrosoftDNS_Server object as we've done earlier in the chapter. Example 27-2 first lists all of the properties of the mycorp.com. zone before it modifies the "AllowUpdate" property and commits the change. Example 27-2. Configuring a zoneon error resume next
strZone = "mycorp.com."
Set objDNS = GetObject("winMgmts:root\MicrosoftDNS")
set objDNSServer = objDNS.Get("MicrosoftDNS_Server.Name="".""")
Set objDNSZone = objDNS.Get("MicrosoftDNS_Zone.ContainerName=""" & strZone & _
""",DnsServerName=""" & objDNSServer.Name & _
""",Name=""" & strZone & """")
' List all of the properties of the zone
Wscript.Echo objDNSZone.Name
for each objProp in objDNSZone.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
' Modify the zone
objDNSZone.AllowUpdate = 1
objDNSZone.Put_
WScript.Echo ""
if Err then
Wscript.Echo "Error occurred: " & Err.Description
else
WScript.Echo "Change successful"
end if
27.3.3 Listing the Zones on a ServerThe last zone example we will show lists the configured zones on a specific DNS server. To make the following example a little more robust, we've added logic to make the script configurable so it can be run against any DNS server. That is accomplished by using the ConnectServer method on the SWbemLocator object. strServer = "dns1.mycorp.com"
strUsername = "dnsadmin"
strPassword = "dnspwd"
Set objLocator = CreateObject("WbemScripting.SWbemLocator")
Set objDNS = objLocator.ConnectServer(strServer, "root\MicrosoftDNS", _
strUsername, strPassword)
set objDNSServer = objDNS.Get("MicrosoftDNS_Server.Name="".""")
set objZones = objDNS.ExecQuery("Select * from MicrosoftDNS_Zone " & _
"Where DnsServerName = '" & _
objDNSServer.Name & "'")
WScript.Echo objDNSServer.Name
for each objZone in objZones
WScript.Echo " " & objZOne.Name
next
To retrieve the list of zones, we used a WQL query with ExecQuery to find all MicrosoftDNS_Zone objects that had a DnsServerName equal to the name of the server we are connecting to. |
| [ Team LiB ] |
|