Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 596
  • Last Modified:

LDAP LookUp via VBScript

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
m_travis
Asked:
m_travis
1 Solution
 
Joseph DalyCommented:
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
 
carsRSTCommented:
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
 
Bill PrewCommented:
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
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

 
m_travisAuthor Commented:
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
 
RobSampsonCommented:
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
 
m_travisAuthor Commented:
Error Message
That is the error I am getting
0
 
RobSampsonCommented:
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
 
m_travisAuthor Commented:
Thank you so much for all the help with this question. One day I will get better at writing VBScripts.
0

Featured Post

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now