/ Published in: Visual Basic
Retrieves Active Directory distinguished name given user's SAMAccountName
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
'=================================================================================== ' FUNCTION GetUserDN ' Purpose: Get user's distinguished name from username '=================================================================================== Function GetUserDN(strUsername) Set oIADS = GetObject("LDAP://RootDSE") strDefaultNC = oIADS.Get("defaultnamingcontext") '===LDAP query string=== 'strBase = "<LDAP://" & strDCName & "/" & strDefaultNC & ">;" strBase = "<LDAP://" & strDefaultNC & ">;" strFilter = "(&(objectclass=user)(objectcategory=person)(sAMAccountName=" & strUsername & "));" strAttrs = "distinguishedName;" strScope = "subtree" '===Run query=== Set objConn = CreateObject("ADODB.Connection") objConn.Provider = "ADsDSOObject" objConn.Open "Active Directory Provider" Set objRS = objConn.Execute(strBase & strFilter & strAttrs & strScope) '===Find user in recordset=== objRS.MoveFirst While Not objRS.EOF strUserDN = objRS.Fields(0).Value objRS.MoveNext Wend GetUserDN = strUserDN End Function