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 


October 2007

Export AD Data into Access So Users Can Run Their Own AD Reports

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

If your organization is anything like mine, you get daily requests from managers for information about the users, computers, and groups in Active Directory (AD). These requests can be simple (e.g., "Can I get a list of all users who are in the New York office?") or detailed (e.g., "Can I get a list of all users with mailboxes on SERVER1 who haven't logged in since April?"). And managers often come back asking for slightly different information or for the information to be presented in a slightly different way. This leaves you—the busy administrator, who has servers to maintain and projects to complete—spending a lot of time tweaking and rerunning scripts.

One skill necessary to be a successful administrator is finding ways to provide people with the means to help themselves, while ensuring that they don't have the ability to break something. Given this goal, I developed a solution that gives managers an easy way to obtain custom AD reports without them having to directly access AD or learn how to write scripts. I export relevant AD data into a Microsoft Access database (although you can just as easily export the information into a SQL database). With this tool, managers can easily run a custom report and tweak it until it returns the data they need in a format they like.

To build this reporting tool, you first need to create a database in Access. If you're unfamiliar with Access, Microsoft offers a series of free Access 2003 courses at http:// office.microsoft.com/en-us/training/ CR061829401033.aspx. For our purposes, I created a sample database that has a single table named ADUsers with four fields: DisplayName, UserID, EmailAddress, and UserDisabled. Each field is a default text field with the exception of UserDisabled, which is a yes/no field. One note of caution: When you're creating your database, make sure the field lengths are large enough to store the information you're gathering. Some AD attribute values are quite long (e.g., dn) and won't fit into Access's default field length. You can download the sample database, ADUsers.mdb, from the Windows IT Pro Web site. (Go to http://www.windowsitpro.com, enter 96855 in the InstantDoc ID text box, then click the Download the Code Here hotlink.)

Now it's time to write the script. Listing 3 shows a sample script named PopulateDB.vbs, which you can also download from the Windows IT Pro Web site. After the script declares the variables and constants, it connects to and queries AD. As callout A in Listing 3 shows, an LDAP query is used. You need to modify this query to reflect your actual domain configuration. LDAP queries include three or four arguments, which are delimited with colons:

  • You use the mandatory first argument (in this example, <LDAP:// dc=mycompany,dc=com>) to specify where in AD you want to start the search. You must use a full path and enclose it in angle brackets (< >).
  • You use the mandatory second argument, which must be enclosed in parentheses, to specify the objects to search for. For example, the (objectCategory=person) argument in Listing 3's query tells the script to search for all user objects derived from a class whose defaultObjectCategory attribute is person.
  • You use the mandatory third argument to specify the attribute to return. In this example, it's the ADsPath attribute, which is used later in the script to bind to each AD user object returned by the query. You can customize the query to return any number of attributes. When you have more than one attribute, you put the attributes in a comma-delimited list.
  • You use the optional fourth argument to specify how far down from the query's starting point you want to search. The options are Subtree (checks every container in the tree), OneLevel (checks objects directly under the root and objects directly under containers in the root), and Base (only checks objects directly in the container).

After executing the query and storing its results in the objRecordset variable, PopulateDB.vbs connects to Access and opens the ADUsers database. In the code at callout B, you need to customize the path to ADUsers. The script then deletes all the records in the database. The database is cleared each time so you don't have to have to search for existing records and determine whether those records have been updated.

In the code at callout C, the script loops through all the AD user objects in the objRecordset variable. After binding to each object using its ADsPath, the script retrieves the values of the displayName, sAMAccountName, mail, and userAccountControl attributes. For each attribute value, the script adds a new record to the database.

To run PopulateDB.vbs, open a command-shell window and run the command

cscript c:\ADUsers\PopulateDB.vbs 

The database can't be open when you launch the script. If it's open, the script will fail. I wrote and tested this script on Windows XP SP2 with Access 2003.

Once all the information is in the database, it's simple to sort and query the data because Access is designed for that purpose. If your managers aren't familiar with Access, you'll need to teach them the basics. Although training them will initially consume some of your time, the return on this investment is worth it because you'll no longer have to spend time every day writing and tweaking scripts for managers.

Listing 4 shows some sample queries for the ADUsers database. The first example generates a list of all disabled user accounts. The second example illustrates how to filter that list so only disabled user accounts that have an email address are selected.

As ADUsers and PopulateDB.vbs demonstrate, you can build a robust reporting tool by importing relevant AD information into a database that people can use to run their own reports. Because you'll no longer have to continually write and rewrite reporting scripts, you'll have more time left for important tasks, such as keeping your environment up to date and secure. In fact, you can even use this tool for some of those tasks. For example, if your users' home directory names match their user IDs, you can use the tool to determine which home directories are no longer in use. You just need to create an additional table named HomeDir, populate a field called HomeDir with a text list of all home directories on your servers, and run the third sample query in Listing 4. The query results will show you the records from HomeDir that don't have user IDs in ADUsers, which indicates those home directories aren't being used.
—Chris Scoggins

End of Article



Reader Comments
Great article

CScoggins October 16, 2007 (Article Rating: )


Loved it. Great suff

stuckeya November 15, 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. ...

Managing Virtual Sprawl

As some wise person once said, nothing is ever truly free. Such is the case with VMs, which can quickly mutate from a cost-reducing Dr. Jekyll into a time-consuming, profligate nightmare that would do Mr. Hyde proud. ...

How can I uninstall the Microsoft Java Virtual Machine (JVM) from Windows XP?

...


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

A Guide to Group Policy

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.


ADS BY GOOGLE SPONSORED LINKS FEATURED LINKS

Critical Challenges of ESI & Email Retention
Are you storing too much electronic information? Get expert legal advice and better understanding of what you are required to do as an IT professional.

Become a fan of Windows IT Pro on Facebook!
Join us on Facebook and be a fan of Windows IT Pro!

Sustainable Compliance: Are You Having a Resource Crisis?
Read this white paper to examine trends in compliance and security management and review approaches to reducing the cost and operational burden of compliance.

Rev Up Your IT Know-How with Our Recharged Magazine!
The improved Windows IT Pro provides trusted IT content with an enhanced new look and functionality! Get comprehensive coverage of industry topics, expert advice, and real-world solutions—PLUS access to over 10,000 articles online. Order today!

Get It All with Windows IT Pro VIP
Stock your IT toolbox with every solution ever printed in Windows IT Pro and SQL Server Magazine plus bonus Web-exclusive content on hot topics. Subscribe to receive the VIP CD and a subscription to your choice of Windows IT Pro or SQL Server Magazine!



Order Your Fundamentals CD Today!
Gain an introduction to Exchange, learn server security requirements, and understand how unified communications can play a role in your messaging strategies with this free Exchange CD.
Windows IT Pro Home Register About Us Affiliates / Licensing Media Kit Contact Us/Customer Service  
SQL Connected Home IT Library SuperSite FAQ Wininfo News
Europe Edition Office & SharePoint Pro Windows Dev Pro Windows Excavator 
 
 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