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 Responses

  1. We need to search a user’s fullname based on login ID. Could you help giving some samples?

    Thank you!

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

    Try

    ‘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)
    Else
    UserInfo = “”
    End If
    End If

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

    Return UserInfo
    End Function

  3. 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!

  4. Jakov,

    Here is a link to a post which has .Net code and also a link to an earlier post which I had written using VBScript
    http://nishantpant.wordpress.com/2008/01/30/enumerate-members-of-an-active-directory-group-in-net/

Leave a Reply