Posted By

lrduncan on 07/13/11


Tagged

directory active vbscript


Versions (?)

FUNCTION GetUserDN


 / Published in: Visual Basic
 

Retrieves Active Directory distinguished name given user's SAMAccountName

  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
Posted By: starworld on July 21, 2011

HI, I am new comer here, I just found this useful site today. I used script above, but when I run it I got the error message The search filter cannot be recognized. when the error reports, the cursor points to line 24. why? thanks!

You need to login to post a comment.