FUNCTION GetUserDN


/ Published in: Visual Basic
Save to your folder(s)

Retrieves Active Directory distinguished name given user's SAMAccountName


Copy this code and paste it in your HTML
  1. '===================================================================================
  2. ' FUNCTION GetUserDN
  3. ' Purpose: Get user's distinguished name from username
  4. '===================================================================================
  5.  
  6. Function GetUserDN(strUsername)
  7. Set oIADS = GetObject("LDAP://RootDSE")
  8. strDefaultNC = oIADS.Get("defaultnamingcontext")
  9.  
  10. '===LDAP query string===
  11. 'strBase = "<LDAP://" & strDCName & "/" & strDefaultNC & ">;"
  12. strBase = "<LDAP://" & strDefaultNC & ">;"
  13. strFilter = "(&(objectclass=user)(objectcategory=person)(sAMAccountName=" & strUsername & "));"
  14. strAttrs = "distinguishedName;"
  15. strScope = "subtree"
  16.  
  17. '===Run query===
  18. Set objConn = CreateObject("ADODB.Connection")
  19. objConn.Provider = "ADsDSOObject"
  20. objConn.Open "Active Directory Provider"
  21. Set objRS = objConn.Execute(strBase & strFilter & strAttrs & strScope)
  22.  
  23. '===Find user in recordset===
  24. objRS.MoveFirst
  25. While Not objRS.EOF
  26. strUserDN = objRS.Fields(0).Value
  27. objRS.MoveNext
  28. Wend
  29. GetUserDN = strUserDN
  30. End Function

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.