August 11, 2005, 4:38 PM —
Written expressly for system administrators, this
book offers tips and techniques that go beyond the
basic management tasks. The hack shown here will
show you how to programmatically search for a user
in a mixed Windows NT/2000 environment.
Author: Mitch Tulloch
ISBN: 0-596-00647-0
Posted with permission of O'Reilly. Click here
for a detailed description and to learn how
to purchase this title.
Hack #25: Search for Domain Users
Programmatically search for a user in a mixed Windows NT/2000 environment.
If you are in the process of migrating from Windows NT to Windows 2000, you can certainly appreciate the search capabilities provided in Active Directory administrative tools. At the same time, more than ever, you suffer from its absence in the User Manager. This issue becomes especially acute in environments where there is no consistent naming convention or when the naming convention happened to change several times over years. The sorting feature might help, but only provided that a person responsible for creating accounts entered the full name correctly and in the same format. Misspellings or using diminutives and nicknames are other frequent causes of confusion. Your search becomes considerably more time consuming if you manage multiple domains with different naming conventions.
To resolve a problem, you can employ a couple of approaches. The first one involves exporting a user list, along with each user's properties, into a comma-delimited file or a database (e.g., Access or SQL). The main drawback of this solution is the need for regular updates of the exported list. The second drawback, which eliminates the need for maintenance, is using an ADSI-based script.
This approach is shown in the script that follows.
The Code
The script allows searches against multiple domains. In order to accomplish this, you need to provide as the second input argument the list of domains (individual names need to be separated by semicolons). The first argument of the script is the part of the username (of any length) that you want to match against account names. Type the script into Notepad (with Word Wrap disabled) and save it with a .vbs extension as FindUser.vbs:













