I am trying to make LDAP queries via Visual Basic. I don't have administrator access to the Active Directory, but I am able to view all user objects. I don't know what restrictions if any there are on me searching the directory via LDAP:// queries.
In the Excel application I am building, I have a column for the input of user IDs. Once a user inputs a user ID, I would like the other columns to be auto-populated based on server side information associated with that user (email address for example)
Let's say c.Value is the User ID value that has been input to the spreadsheet:
strUser = "CN=" & c.Value & ",OU=User Accounts,OU=Area,OU=Users,OU=Accounts,DC=joe,DC=bloggs,DC=co,DC=uk/"
Set objUser = GetObject("LDAP://" & strUser)
The problem is this -- where OU=Area is known, the search is successful. However, I would like the query to check all area OUs for the UserID. As far as I can tell they are not held or mirrored in a central location. Is it possible to use Wildcards in such a query?
Does anyone have any other ideas or suggestions as to alternate ways to go about this?
Thank you,
Tom
Getting the Components of an LDAP URLTo get these attributes as an enumeration, use the getAttributes method. To get the hostname of the LDAP server, use the getHost method. To get the port number of the LDAP server, use the getPort method. To get the base DN, use the getDN method.
All LDAP URLs must include a scheme followed by a colon and two forward slashes (e.g., “ldap://”). The address and/or port of the target directory server. The address may be an IPv4 or IPv6 address or a resolvable name.
Examples of LDAP URLs The following LDAP URL specifies a base search for the entry with the distinguished name dc=example,dc=com. Because no port number is specified, the standard LDAP port number (389) is used. Because no attributes are specified, the search returns all attributes.
Before I answer your question, here are some basic background knowlege on Active Directory.
To execute a LDAP query, you need to use an ADO connection object. You need to pass in a LDAP query string to the ADO connection object. The LDAP query string contains four parts.
The LDAP query string that you should use should be something like
<LDAP://OU=Users,OU=Accounts,DC=joe,DC=bloggs,DC=co,DC=uk>;(&(objectClass=user)(samAccountName=yourusername));adspath;subtree
<LDAP://OU=Users,OU=Accounts,DC=joe,DC=bloggs,DC=co,DC=uk>
. It means start searching at this level(&(objectClass=user)(samAccountName=yourusername))
. Of course, you need to replace yourusername
to something else inside your code. If you really want to do the search on CN, change it to CN hereadspath
, which allows you to bind to that object latersubtree
Here is a complete sample that I guess it should do your job
userName = "harvey"
ldapStr = "<LDAP://OU=Users,OU=Accounts,DC=joe,DC=bloggs,DC=co,DC=uk>;(&(objectClass=user)(samAccountName=" & userName & "));adspath;subtree
Set conn = CreateObject("ADODB.Connection")
conn.Provider = "ADSDSOObject"
conn.Open "ADs Provider"
Set rs = conn.Execute(ldapStr)
While Not rs.EOF
wscript.echo rs.Fields(0)
rs.MoveNext
Wend
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With