I created a time-saving utility named GetADsPath, which returns the ADsPath strings of Active Directory (AD) objects. When you enter an object's name (CN, sAMAccountName, or GroupName attribute), the utility returns the ADsPath of that object. You can then copy the ADsPath into any code that requires an ADsPath.
For example, let's say you have an object named XPDuplicator and you don't know its ADsPath. With the GetADsPath utility, you simply enter XPDuplicator into the utility's UI and click the Get AdsPath & OU button, as Figure 1 shows. You can then copy that ADsPath and paste it into code such as
("LDAP://CN=XPDuplicator," & _ "OU=Unique,OU=USAssets," & _
"OU=Workstations," & _
"OU=All Computers,DC=ad," & _ "DC=mycompany,DC=com")
Besides the ADsPath, the utility returns the organizational unit (OU) an object is in. You should be able to key in any valid AD object and receive an ADsPath. However, not all objects have an OU, so that field might be blank.
When a user enters an AD object's name and clicks the Get AdsPath & OU button, the GetADSPathOU subroutine executes. As Listing 1 shows, after the subroutine declares its constant, it sets up a variable that will contain the domain's name. This makes the code more generic so that you don't have to hard-code the domain information. The next several lines of code set up the connection to the AD database using ActiveX Data Objects (ADO). The subroutine creates an ADO Connection object to connect to AD, an ADO Command object that will contain the query, and an ADO Recordset object that will contain the resulting records.
The heart of the code is the SQL query that's stored in the Command object's CommandText property, which callout A in Listing 1 shows. The utility uses that query to search AD for the object that contains the specified name. If an object is found, the utility displays the ADsPath. If an OU exists, it's extracted from the ADsPath and displayed. When there are multiple OUs in an ADsPath, the utility extracts the first OU.
To extract the OU, the utility breaks the ADsPath into an array, using a comma as a delimiter, as callout B in Listing 1 shows. In some instances, the OU will be in the first element of the array (e.g., LDAP://OU=BranchOffice Users,OU=Domain Users). In other instances, the OU will be the second element of the array (e.g., LDAP:// CN=XPDuplicator,OU=Unique). When the OU is in the first array element, there will be 10 characters before the OU's name. When the OU is in the second array element, there will be 3 characters before the OU's name. Thus, the utility uses the IIf (short for Immediate If) function to determine how many characters to skip (i.e., 3 or 10) to get to where the OU name starts. (For information about the IIf function, see "A VBScript Version of VBA's IIf Function," May 2005, InstantDoc ID 45816.)
You can download the GetADsPath utility from the Windows Scripting Solutions Web site. This utility works on Windows Server 2003, Windows XP, and Windows 2000 machines. You need to have AD, Windows Script Host (WSH) 5.6, and Microsoft Internet Explorer (IE) 4.0 or later.
The GetADsPath utility can save you time because you don't need to search through AD looking for a particular ADsPath. Even if you do know a complete ADsPath, the utility can save you the hassle of typing the long path and possibly avoid problems that might occur because of typos.—Jim Turner