LDAP Query to search a user in AD using his partial last name

Dim User as object

Set User = GetUserObjectFromPartialName(‘doe’)

Msgbox User.FullName & User.sAMAccountName


Public Function GetUserObjectFromPartialName(ByVal PartialName As String) As Object
On Error Resume Next
    Set rs = CreateObject(“adodb.recordset”)
    rs.ActiveConnection = “provider=adsdsoobject”
    rs.Open “<LDAP://dc=sl,dc=ad,dc=csx,dc=com>;(&(objectCategory=Person)” & _
    “(objectClass=OrganizationalPerson)(name=” & PartialName & “*));adspath;subtree”

    If Not rs.EOF Then
        Set GetUserObjectFromPartialName = GetObject(rs(0))
    End If
End Function


4 thoughts on “LDAP Query to search a user in AD using his partial last name

  1. Here is the code in vb.net which will do what you are looking for. You will notice that this function retrieves many more attributes from Active Directory other than user’s fullname. So, you can play around with it.

    Public Shared Function GetUserInfo() As String
    Dim userAndDomain As String = HttpContext.Current.User.Identity.Name.ToUpper()
    Dim user As String = userAndDomain.Replace(“SL\”, “”)
    Dim SLSearchPath, ADUser, ADPwd As String
    If InStr(user, “\”) 0 Then
    user = Mid(user, InStr(user, “\”) + 1)
    End If
    SLSearchPath = ConfigurationSettings.AppSettings(“LDAP_ADSL_SearchPath”)
    ADUser = ConfigurationSettings.AppSettings(“ADUser”)
    ADPwd = ConfigurationSettings.AppSettings(“ADPassWord”)
    Dim UserInfo As String


    ‘Get the user details from active directory and database
    Dim ds As New DirectoryServices.DirectorySearcher( _
    New DirectoryServices.DirectoryEntry(SLSearchPath, ADUser, ADPwd), _
    “(&(objectCategory=User)(sAMAccountName=” & user & “))”, _
    New String() {“displayName”, _
    “telephoneNumber”, _
    “mail”, _
    “title”, _
    “department”, _
    “description”, _
    “l”, _
    “givenName”, _
    “sn”, _
    “st”}, DirectoryServices.SearchScope.Subtree)
    Dim sr As DirectoryServices.SearchResult = ds.FindOne()
    If Not sr Is Nothing Then
    If Not sr.Properties(“DisplayName”)(0) Is Nothing Then
    UserInfo = CType(sr.Properties(“DisplayName”)(0), String)
    UserInfo = “”
    End If
    End If

    Catch Ex As Exception
    Throw New Exception(“Error reading user information from active directory” & Ex.Message)
    End Try

    Return UserInfo
    End Function

  2. How to create such kind of LDAP Query or VBS script, which will list all the groups and the members of a group in a specified domain, also in case if one of the member is a group.

    Thank you!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s