Recipe15.16.Finding the Closest Domain Controller


Recipe 15.16. Finding the Closest Domain Controller

Problem

You want to find the closest domain controller for a particular domain.

Solution

Using a command-line interface

The following command finds the closest domain controller in the specified domain (<DomainDNSName>). By default, it will return the DC closest to the computer nltest is being run from, but you can optionally use the /server option to target a remote computer. You can also optionally specify the /site option to find a domain controller that belongs to a particular site.

> nltest /dsgetdc:<DomainDNSName> [/site:<SiteName>] [/server:<ClientName>]

Using VBScript
' This code finds the closest domain controller in the domain ' that the computer running the script is in. ' ------ SCRIPT CONFIGURATION ------ strDomain = "<DomainDNSName>"  ' e.g., emea.rallencorp.com ' ------ END CONFIGURATION ---------     set objIadsTools = CreateObject("IADsTools.DCFunctions") objIadsTools.DsGetDcName( Cstr(strDomain) ) Wscript.Echo "DC: " & objIadsTools.DCName Wscript.Echo "DC Site: " & objIadsTools.DCSiteName Wscript.Echo "Client Site: " & objIadsTools.ClientSiteName

Discussion

The DC locator process as described in MS KB 314861 and MS KB 247811 defines how clients find the closest domain controller. The process uses the site topology stored in Active Directory to calculate the site where a particular client is. After the client site has been identified, it is a matter of finding a domain controller that is either a member of that same site or that is covering that site.

The Microsoft DsGetDcName Directory Services API method implements the DC Locator process, but unfortunately cannot be used directly from a scripting language, such as VBScript. The IADsTools interface provides a wrapper around DsGetDcName, which is what I used. The nltest /dsgetdc command is also a wrapper around the DsGetDcName method, and is a handy tool when troubleshooting client issues related to finding an optimal domain controller.

Using a command-line interface

You can use nltest to return the closest domain controller that is serving a particular function. Some of the available functions include a global catalog server (/GC option), time server (/TIMESERV option), KDC (/KDC option), and PDC (/PDC option). Run nltest /? from a command line for the complete list.

Using VBScript

As with nltest, you can specify additional criteria for finding a domain controller by calling the SetDsGetDcNameFlags method before calling DsGetDcName. SetDsGetDcNameFlags accepts a comma-delimited string of the following flags:

  • DS_FORCE_REDISCOVERY

  • DS_DIRECTORY_SERVICE_REQUIRED

  • DS_DIRECTORY_SERVICE_PREFERRED

  • DS_GC_SERVER_REQUIRED

  • DS_PDC_REQUIRED

  • DS_IP_REQUIRED

  • DS_KDC_REQUIRED

  • DS_TIMESERV_REQUIRED

  • DS_WRITABLE_REQUIRED

  • DS_GOOD_TIMESERV_PREFERRED

  • DS_AVOID_SELF

  • DS_IS_FLAT_NAME

  • DS_IS_DNS_NAME

  • DS_RETURN_DNS_NAME

  • DS_RETURN_FLAT_NAME

See Also

For more information on the IADsTools interface, see IadsTools.doc in the Support Tools; MS KB 247811 (How Domain Controllers Are Located in Windows); MS KB 314861 (How Domain Controllers Are Located in Windows XP); and MSDN: DsGetDcName, and MSDN: MicrosoftDNS



Windows Server Cookbook
Windows Server Cookbook for Windows Server 2003 and Windows 2000
ISBN: 0596006330
EAN: 2147483647
Year: 2006
Pages: 380
Authors: Robbie Allen

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net