Skip to content

Instantly share code, notes, and snippets.

@trevordevore
Last active March 2, 2017 13:45
Show Gist options
  • Save trevordevore/9ff3ac35bd6a2eef2f3d to your computer and use it in GitHub Desktop.
Save trevordevore/9ff3ac35bd6a2eef2f3d to your computer and use it in GitHub Desktop.
result = ""
theDomain = "[[Domain]]" 'domain with no trailing /
theQuery = "[[Query]]" 'query such as ou=Users,dc=...
theUser = "[[Username]]"
thePassword = "[[Password]]"
On Error resume next
Const E_ADS_PROPERTY_NOT_FOUND = &h8000500D
LDAPString = "LDAP://"
if not IsEmpty(theDomain) Then
LDAPString = LDAPString & theDomain & "/"
End If
LDAPString = LDAPString & "[[UserKey]]=" & theUser
If not IsEmpty(theQuery) Then
LDAPString = LDAPString & "," & theQuery
End If
Dim LDAP, lUser
Set LDAP = GetObject("LDAP:")
Set lUser = LDAP.OpenDSObject(LDAPString, theUser, thePassword, 0)
If Err.Number <> 0 Then
result = "error," & Err.Number ' default value
' http://www.selfadsi.org/errorcodes.htm
Select Case Err.Number
Case -2147463168
result = "error,incorrect LDAP path"
Case -2147023570
result = "error,invalid login credentials"
Case -2147463160
result = "error,bad parameter"
Case -2147217911
result = "error,permission denied"
Case -2147024891
result = "error,insufficient rights"
Case -2147023541
result = "error,domain does not exist"
Case -2147016671
result = "error,protocol error"
Case -2147016656
result = "error,no such object"
Case -2147016646
result = "error,LDAP server is unavailable"
End Select
End If
if Err.Number = 0 Then
' Look for display name
If IsObject(lUser) Then
If lUser.Class = "user" or lUser.Class = "userProxy" Then
result = lUser.displayName
' Get groups use is member of
' Tab delimited on line 2
lUserGroups = lUser.GetEx("memberOf")
'If IsObject(lUserGroups) Then
If Err.Number <> E_ADS_PROPERTY_NOT_FOUND Then
result = result & vblf
For Each objGroup in lUserGroups
result = result & objGroup & vbTab
Next
End If
End If
End If
End If
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment