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 


November 13, 2006

Quickly Finding User Accounts Anywhere in an Active Directory Forest


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!
Main Article    Rem

Download the Code Here

I work in a network that contains a single forest made up of many Active Directory (AD) domains. I need to verify that a user account exists and in what domain it exists. I know I can use Active Directory Users and Computers to find an account. How can I script this task instead?
You can accomplish this task by writing a script that queries a global catalog server in your forest. Because each global catalog server in a forest contains a partial replica of all objects in all domains within the forest, it's the best place for locating AD objects such as user accounts. Listing 1, shows how you can use the ADSI OLE DB provider to search a global catalog server in a forest for a user account whose common name (cn attribute) begins with the letters eth. To make this script a bit more dynamic, I bind to the rootDSE object.

The rootDSE object contains the base Directory System Agent (DSA) information from any domain controller in the domain. DSA information includes data such as the current DNS host name and the various naming contexts in an AD implementation, such as the default schema, and configuration naming contexts. The code at callout A in Listing 1 demonstrates how to bind to the rootDSE, retrieve values from this object (in this case, the script retrieves the distinguished name of the forest, which is stored in the rootDomainNamingContext), and create a variable containing an AdsPath that uses the GC moniker. The GC moniker instructs the script to connect to any domain controller serving as a global catalog server. Under the covers, the GC moniker makes a connection request by using port 3268, which is the listening port for all AD global catalog servers. The actual bind operation performs authentication to an available global catalog server by using the credentials of the currently logged-on user.

The next section of the script demonstrates how to set up a search operation. The most interesting part is constructing the value passed to the CommandText property, which you can see in callout B of Listing 1. The value passed to the property includes four parts:

  1. The AdsPath to a global catalog. This is the part of the command that contains information about where to start the search operation. In this case, we're starting at the root of the forest.
  2. The LDAP filter to limit results. In this case, the filter limits results to user accounts that have a name starting with the value stored in the commonNamePart variable.
  3. The attribute or attributes to return in the result set. In this case, limit the returned results to the distinguishedName attribute of any user account meeting the search criteria defined by the LDAP filter.
  4. How many levels to search— base, onelevel, or subtree. In this case, the code performs a subtree search to return results from the entire directory starting at the specified AdsPath and searching to the bottom-most domain in the forest.

The last part of the script simply iterates any returned results and displays the distinguished name of any user account meeting the specified criteria. The code download for this article contains a completed Windows Script File (WSF) version of the script you see in Listing 1. I strongly recommend that you download the script sample rather than using the simplified code that Listing 1 shows. The code download demonstrates how to pass in any attribute for your search operation, such as a user's first name (givenName attribute) or last name (sn attribute) rather than using the cn attribute in Listing 1. To call the WSF script, use the following example syntax:

Finduser.wsf
  /attrib:sAMAccountName
  =ewilansky 
Finduser.wsf
  /attrib:displayName=ste*

End of Article



Reader Comments
I receive an invalid entry reference message at Line 27 Char 28.

Commandline used:
finduser.wsf /attrib:displayName=cr*

nitz@pabgames.net November 28, 2006 (Article Rating: )


Try running the command by appending cscript in front of finduser.wsf: cscript finduser.wsf /attrib:displayName=cr*
Also, are you sure you're running Windows Script Host 5.6?

EthanW December 19, 2006 (Article Rating: )


I do not see the .zip file link at the beginning of the article do download the code as you recommend. I am a subscriber, could you please send me the link? Listing 1 as is always returns "No users found with that name" but I know that in my forest they exist (I do edit commonNamePart). Thank you, it would be great if I can get it to work.

ppierani December 19, 2006 (Article Rating: )


It would be more useful if I could find the link to download the code.

sogadmin January 30, 2007 (Article Rating: )


It's interesting that it is "recommended" to download the script yet there is no link for the sourcecode to be found. <sigh>

gwojan February 08, 2007 (Article Rating: )


To download the zip file containing the script, click the Download the Code button at the beginning of the article (top left, under the title info). The direct link is <a href="http://www.windowsitpro.com/Files/07/94027/94027.zip">here</a>.
Anne Grubb, senior editor, Windows IT Pro

AnneG_editor May 09, 2007 (Article Rating: )


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. ...

Friday at PASS Europe 2006

Kevin talks about the closing day of the event and shares a funny Microsoft film. ...

Escape From Yesterworld

Kevin points you to the funniest SQL Server website ever! ...


Related Articles Copy Group Memberships from One AD User to Another

Active Directory (AD) Whitepapers An Introduction to Windows Server 2008 Server Manager

Get More from Active Directory—Easily Audit Changes, and Secure and Restore Objects

User Provisioning: Get the Most Bang for your IT Buck

Related Events Check out our list of Free Email Newsletters!

Active Directory (AD) eBooks Keeping Your Business Safe from Attack: Monitoring and Managing Your Network Security

Keeping Your Business Safe from Attack: Encryption and Certificate Services

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.

Job Openings in IT


ADS BY GOOGLE SPONSORED LINKS FEATURED LINKS

Maximize your SharePoint Investment – 8 Cities
Discover best practices and tips for both architecting and administering SharePoint. Early Bird Price of $99 through Sept 15th.

Find a new job now on the all new IT Job Hound!
Search jobs, post your resume, and set up job e-mail alerts!

Master SharePoint with 3 eLearning Seminars
Learn how to build a better SharePoint infrastructure and enable powerful collaboration with MVPs Dan Holme and Michael Noel. Register today!

Top Tools for Virtualization Disaster Recovery & Replication
View this web seminar on August 14th to learn about two tools that will result in faster backup and restore with P2V disaster recovery.

SharePointConnections Conference Fall 2008
Don’t miss the premier event for Microsoft IT Professionals in Las Vegas, November 10-13. Register and book your room by August 25 and receive a FREE room night (based on a three night minimum stay).

VMworld 2008 - Sign Up Today!
Join your peers on September 15-18 at The Venetian Hotel in Las Vegas as VMware hosts VMworld 2008, the leading Virtualization event.



Entrust Unified Communications Certs
Secure Exchange 2007 and save 20%. Now through Sept. 2008.

Increase Application Performance
Free White Paper by Editor's Best winner, Texas Memory Systems.

Need to convert between XML, DBs, EDI, and Excel? Try MapForce free!
Drag & drop to transform between popular data formats – get results instantly or generate code.

Microsoft® Tech•Ed EMEA 2008 IT Professionals
Advance your thinking with new ideas and practical real-world solutions at Microsoft’s FIVE day technical infrastructure conference 3-7 Nov., 2008. Register before 26 September 2008 to save €300.

Order Your SQL Fundamentals CD Today!
Learn how to use SQL Server, understand Office integration techniques and dive into the essentials of SQL Express and Visual Basic with this free SQL Fundamentals CD.

Are You Really Compliant with Software Regulations?
View this web seminar that will help you with compliance best practices and check out a management solution to assure that you won’t be in jeopardy of an audit.

Virtualization Congress Oct. 14-16 in London
Don't miss Virtualization Congress, the premiere EMEA conference dedicated to hardware, OS and application virtualization. Oct. 14-16.
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 Technical Resources 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