Windows IT Pro is the authoritative and independent resource for windows nt, windows 2000, windows 2003, windows xp. Features a collection of resources and magazines for windows IT professionals.
  
  
  Advanced Search 


July 11, 2006

Get ADsPaths with the Click of a Button

RSS
View this exclusive article with VIP access -- click here to join |
See More Active Directory (AD) Articles Here | Reprints | Or sign up for our VIP Monthly Pass!

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 log on before posting a comment.

If you don't have a username & password, please register now.




Top Viewed ArticlesView all articles
The Memory-Optimization Hoax

Don't believe the hype. At best, RAM optimizers have no effect. At worst, they seriously degrade performance. ...

Command Prompt Tricks

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

SET Options and Recompilation

Learn how to tweak your server's SET options so that you don't have to constantly recompile. ...


Active Directory (AD) Whitepapers Sustainable Compliance: How to reconnect compliance, security and business goals

Managing Unix/Linux with Microsoft System Center Operations Manager 2007 Cross Platform Extensions Beta

Addressing the Insider Threat with NetIQ Security and Administration Solutions

Related Events Concrete Ways to Make Sure Your SharePoint Deployment Doesn't Blow Up

PCI Requirements for Windows and Active Directory: Straight from a Certified Auditor

Check out our list of Free Email Newsletters!

Task Automation eBooks Spam Fighting and Email Security for the 21st Century

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

Windows 2003: Active Directory Administration Essentials

Related Active Directory (AD) Resources Become a VIP member of the Windows IT Pro community!
Get it all with the VIP CD and VIP access. A $500+ value for only $279!

Subscribe to Windows IT Pro!
Solve your toughest technical problems with our experts and access 10,000 + articles online. 30% off

Monthly Online Pass - Only $5.95!
Get instant access to 10,000+ articles from Windows IT Pro Magazine!

TechNet Virtual Labs
Evaluate and test Microsoft's newest products.


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 Windows Dev Pro IT Job Hound ITTV
IT Library Technology Resource Directory Connected Home Windows Excavator Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 Copyright © 2008 Penton Media, Inc., All rights reserved. Terms and Use | Privacy Statement | Reprints and Licensing