Return to Snippet

Revision: 48933
at July 13, 2011 02:28 by lrduncan


Initial Code
'===================================================================================
' 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

Initial URL

                                

Initial Description
Retrieves Active Directory distinguished name given user's SAMAccountName

Initial Title
FUNCTION GetUserDN

Initial Tags
directory

Initial Language
Visual Basic