Solved

LDAP LookUp via VBScript

Posted on 2012-04-03
8
591 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 54

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
Office 365 Training for IT Pros

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

 
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
 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Always backup Domain, SYSVOL etc.using processes according to Microsoft Best Practices. This is meant as a disaster recovery process for small environments that did not implement backup processes and did not run a secondary domain controller that ne…
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
This tutorial will walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

752 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