Search for a User with a Specified Employee ID

Submitted By: Jeremy McGuinn

Language: VBScript

Description: Searches Active Directory for a specified Employee ID.

Script Code

ADS_CHASE_REFERRALS_SUBORDINATE = &h20
strEmpID = InputBox("Enter Employee ID")

dtStart = TimeValue(Now())
Set objConnection = CreateObject("ADODB.Connection")
objConnection.Open "Provider=ADsDSOObject;"
 
Set objCommand = CreateObject("ADODB.Command")
objCommand.ActiveConnection = objConnection
 
objCommand.Properties("Chase Referrals") = _ 
   ADS_CHASE_REFERRALS_SUBORDINATE
 
objCommand.CommandText = _
    "<LDAP://dc=domainname,dc=org>;(&(objectCategory=person)(objectClass=user)" & _
    "(employeeid=" & strEmpID & "));employeeid,name;subtree"
       
Set objRecordSet = objCommand.Execute
 
If objRecordset.RecordCount = 0 Then
    WScript.Echo "The employeeID: " & strEmpID & " does not exist."
Else
    WScript.Echo "The employeeID was found and belongs to user "  & objRecordset.fields("name")
    
End If
 
objConnection.Close

Note: Not all scripts run on all versions of Windows.

For online peer support, join The Official Scripting Guys Forum! To provide feedback or report bugs in sample scripts or on the Script Center, please contact scripter@microsoft.com.

Disclaimer

This script is not supported under any Microsoft standard support program or service. The script is provided AS IS without warranty of any kind. Microsoft further disclaims all implied warranties including, without limitation, any implied warranties of merchantability or of fitness for a particular purpose. The entire risk arising out of the use or performance of the script and documentation remains with you. In no event shall Microsoft, its authors, or anyone else involved in the creation, production, or delivery of the script be liable for any damages whatsoever (including, without limitation, damages for loss of business profits, business interruption, loss of business information, or other pecuniary loss) arising out of the use of or inability to use the script or documentation, even if Microsoft has been advised of the possibility of such damages.


Top of pageTop of page