Solved

LDAP LookUp via VBScript

Posted on 2012-04-03
8
586 Views
Last Modified: 2012-04-19
I need to search Active Directory by last name or email and pull the employeeID attribute which is a hidden attribute and the users mobile phone. I am able to pull the mobile phone but I can't seem to pull the employeeID attribute.
0
Comment
Question by:m_travis
8 Comments
 
LVL 35

Expert Comment

by:Joseph Daly
ID: 37802619
If your are just looking to get this information and it doesnt need to be in VB then you can use the Quest cmdlets to get what you are looking for. The command below should work for you.

get-qaduser -lastname "lastname" -includeallproperties | select-object employeeid, mobile
0
 
LVL 16

Expert Comment

by:carsRST
ID: 37802636
You'll have to play with the attributes but here are a couple of functions I use.

You'll set a reference to "Active DS Type Library"

Fill in your domain name.

Public Function isEmployee(ByVal usernameToCheck As String) As Boolean

    On Error GoTo errH

    Dim user As IADsUser

    Dim grp As Object
   
    Set user = GetObject("WinNT://" & DomainName & "/" & usernameToCheck & ",user")

    isEmployee = True
Exit Function
errH:
    If Err.Number = -2147022675 Then
        isEmployee = False
        Exit Function
    End If
    Err.Raise Err.Number, , Err.Description
End Function




Public Function getUserInfo(ByVal usernameToCheck As String) As String
    On Error GoTo errH

    Dim user As IADsUser

    Dim grp As Object

    Set user = GetObject("WinNT://" & DomainName & "/" & usernameToCheck & ",user")

    retVal = ""
    
    retVal = retVal & user.FullName & vbCrLf & user.Description
    
    getUserInfo = retVal
    
Exit Function
errH:
    If Err.Number = -2147022675 Then
        getUserInfo = "Not a valid user"
        Exit Function
    End If
    Err.Raise Err.Number, , Err.Description
End Function

Open in new window

0
 
LVL 52

Expert Comment

by:Bill Prew
ID: 37802681
I apologize if you truly need a VBS scripting solution, but I use this tool often enough for simple queries like this that I have to mention it.  You might take a look at the free ADFIND utility at the following location, great for this stuff.

http://www.joeware.net/freetools/tools/adfind/index.htm

~bp
0
 
LVL 1

Author Comment

by:m_travis
ID: 37802954
I am getting this error
"Error: Item cannot be found in the collection corresponding to the requested name or ordinal."

Here is the code
Option Explicit

Dim adoCommand, adoConnection, strBase, strFilter, strAttributes, strEmail
Dim objRootDSE, strDNSDomain, strQuery, adoRecordset, strName, strMobile

' Prompt for email address.
strEmail = InputBox("Enter email address of user")

' Setup ADO objects.
Set adoCommand = CreateObject("ADODB.Command")
Set adoConnection = CreateObject("ADODB.Connection")
adoConnection.Provider = "ADsDSOObject"
adoConnection.Open "Active Directory Provider"
Set adoCommand.ActiveConnection = adoConnection

' Search entire Active Directory domain.
Set objRootDSE = GetObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.Get("defaultNamingContext")
strBase = "<LDAP://" & strDNSDomain & ">"

' Filter on user with specified email address.
strFilter = "(mail=" & strEmail & ")"

' Comma delimited list of attribute values to retrieve.
strAttributes = "sAMAccountName,mobile"

' Construct the LDAP syntax query.
strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
adoCommand.CommandText = strQuery
adoCommand.Properties("Page Size") = 200
adoCommand.Properties("Timeout") = 30
adoCommand.Properties("Cache Results") = False

' Run the query.
Set adoRecordset = adoCommand.Execute

' Enumerate the resulting recordset.
Do Until adoRecordset.EOF
  ' Retrieve values and display.
  strName = adoRecordset.Fields("sAMAccountName").Value
  strMobile = adoRecordset.Fields("mobile").Value
  strEmployeeID = adoRecordSet.Fields("employeeID").Value
  Wscript.Echo "NT Name: " & strName & ", Mobile phone number: " & strMobile & ", Employee ID:" & strEmployeeID
  ' Move to the next record in the recordset.
  adoRecordset.MoveNext
Loop

' Clean up.
adoRecordset.Close
adoConnection.Close

Open in new window

0
Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

 
LVL 65

Expert Comment

by:RobSampson
ID: 37803868
That error is because you are trying to extract "employeeID" with this line:
  strEmployeeID = adoRecordSet.Fields("employeeID").Value


but you're not querying for it with this line:
strAttributes = "sAMAccountName,mobile"

so change that line to:
strAttributes = "sAMAccountName,mobile,employeeID"

and you should be fine.

Regards,

Rob.
0
 
LVL 1

Author Comment

by:m_travis
ID: 37833943
Error Message
That is the error I am getting
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
ID: 37835274
Sorry, we forgot to declare it.  Try this.

Rob.

Option Explicit

Dim adoCommand, adoConnection, strBase, strFilter, strAttributes, strEmail
Dim objRootDSE, strDNSDomain, strQuery, adoRecordset, strName, strMobile, strEmployeeID

' Prompt for email address.
strEmail = InputBox("Enter email address of user")

' Setup ADO objects.
Set adoCommand = CreateObject("ADODB.Command")
Set adoConnection = CreateObject("ADODB.Connection")
adoConnection.Provider = "ADsDSOObject"
adoConnection.Open "Active Directory Provider"
Set adoCommand.ActiveConnection = adoConnection

' Search entire Active Directory domain.
Set objRootDSE = GetObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.Get("defaultNamingContext")
strBase = "<LDAP://" & strDNSDomain & ">"

' Filter on user with specified email address.
strFilter = "(mail=" & strEmail & ")"

' Comma delimited list of attribute values to retrieve.
strAttributes = "sAMAccountName,mobile,employeeID"

' Construct the LDAP syntax query.
strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
adoCommand.CommandText = strQuery
adoCommand.Properties("Page Size") = 200
adoCommand.Properties("Timeout") = 30
adoCommand.Properties("Cache Results") = False

' Run the query.
Set adoRecordset = adoCommand.Execute

' Enumerate the resulting recordset.
Do Until adoRecordset.EOF
  ' Retrieve values and display.
  strName = adoRecordset.Fields("sAMAccountName").Value
  strMobile = adoRecordset.Fields("mobile").Value
  strEmployeeID = adoRecordSet.Fields("employeeID").Value
  Wscript.Echo "NT Name: " & strName & ", Mobile phone number: " & strMobile & ", Employee ID:" & strEmployeeID
  ' Move to the next record in the recordset.
  adoRecordset.MoveNext
Loop

' Clean up.
adoRecordset.Close
adoConnection.Close

Open in new window

0
 
LVL 1

Author Closing Comment

by:m_travis
ID: 37866883
Thank you so much for all the help with this question. One day I will get better at writing VBScripts.
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction You may have a need to setup a group of users to allow local administrative access on workstations.  In a domain environment this can easily be achieved with Restricted Groups and Group Policies. This article will demonstrate how to…
Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
The viewer will learn how to dynamically set the form action using jQuery.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now