Windows IT Pro is the leading independent community for IT professionals deploying Microsoft Windows server and client applications and technologies.
  
  
  Advanced Search 


July 11, 2006

Get ADsPaths with the Click of a Button

RSS
Subscribe to Windows IT Pro | See More Active Directory (AD) Articles Here | Reprints | Or get the Monthly Online Pass—only $5.95 a month!

Download the Code Here

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

Set objObject = GetObject _ 

("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

End of Article



Reader Comments

You must be a registered user or online subscriber to comment on this article. Please log on before posting a comment. Are you a new visitor? Register now




Top Viewed ArticlesView all articles
WinInfo Short Takes: Week of November 9, 2009

An often irreverent look at some of the week's other news, including some more Windows 7 sales momentum, some Sophos stupidity, Microsoft's cloud computing self-loathing, more whining from the browser makers, Zoho's "Fake Office," and much, much more ...

Command Prompt Tricks

One reader shares his tip for setting up the command prompt to reflect a remote path. ...

Understanding File-Size Limits on NTFS and FAT

A general confusion about files sizes on FAT seems to stem from FAT32's file-size limit of 4GB and partition-size limit of 2TB. ...


Active Directory (AD) Whitepapers Meeting Compliance Objectives in SharePoint

Email Controls and Regulatory Compliance

Continuous Data Protection and Recovery for Microsoft Exchange

Related Events WinConnections and Microsoft® Exchange Connections

Troubleshooting Active Directory

7 Ways To Get More From Your SharePoint Deployment Now

Check out our list of Free Email Newsletters!

Active Directory (AD) eBooks The Essentials Series: Active Directory 2008 Operations

Keeping Your Business Safe from Attack: Monitoring and Managing Your Network Security

Windows 2003: Active Directory Administration Essentials

Related Active Directory (AD) Resources Introducing Left-Brain.com, the online IT bookstore
Looking for books, CDs, toolkits, eBooks? Prime your mind at Left-Brain.com

Discover Windows IT Pro eLearning Series!
Clear & detailed technical information and helpful how-to's, all in our trademark no-nonsense format


Windows IT Pro Home Register FAQ for Windows WinInfo News
Europe Edition About Us Contact Us/Customer Service Media Kit Affiliates / Licensing  
SQL Server Magazine Office & SharePoint Pro DevProConnections IT Job Hound
Left-Brain.com Technology Resource Directory asp.netPRO ITTV Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 © 2009 Penton Media, Inc. Terms of Use | Privacy Statement