Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Query Active Director based on prompted username

Posted on 2013-11-13
8
Medium Priority
?
266 Views
Last Modified: 2013-11-15
I need to query some Active Directory information based on a username being entered.

I used this which works great for the currently logged in user:

Set ADSysInfo = CreateObject("ADSystemInfo")
Set CurrentUser = GetObject("LDAP://" & ADSysInfo.UserName)

Open in new window


I tried this but it came back with "no such object".  Not sure what I'm supposed to be passing in place userInitials.

userInitials = InputBox("Enter User's Initials")
Set CurrentUser = GetObject("LDAP://CN=" & userInitials & ",OU=Current Users,DC=domain,DC=com")

Open in new window


How do I substitute ADSysInfo.UserName for the prompted userInitials?
0
Comment
Question by:mcnuttlaw
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 9

Expert Comment

by:WebDevEM
ID: 39645133
Hi,

I don't have an LDAP server I can test this against at the moment, but I found a list of the properties that may help you (here)  If I'm reading that right, you'd want to use "initials" instead of "CN" (which is their full name)
userInitials = InputBox("Enter User's Initials")
Set CurrentUser = GetObject("LDAP://initials=" & userInitials & ",OU=Current Users,DC=domain,DC=com")

Open in new window

Hope this helps!

WebDevEM
0
 
LVL 2

Author Comment

by:mcnuttlaw
ID: 39645209
Thanks but it did not work.

I also tried uid.
0
 
LVL 9

Expert Comment

by:WebDevEM
ID: 39645256
Are you replacing your own directory information in the rest of the GetObject line, or leaving the example ",OU=Current Users,DC=domain,DC=com" in there?  If you leave that example text in, your query will look for somebody matching the given initials only if they are in a group named "Current Users" within "domain.com"

You could probably get away with leaving that section out - I have some old code I use which queries an LDAP server as if it was a SQL database, and my query to retrieve all users is
SELECT ipphone, homephone, mobile, name, mail, telephonenumber, givenName, sn, userAccountControl, extensionattribute1,extensionattribute2 FROM 'LDAP://" & LDAPServer & "' WHERE objectClass='*' AND ObjectCategory='user' ORDER BY sn

Open in new window

so in your case this might work:
userInitials = InputBox("Enter User's Initials")
Set CurrentUser = GetObject("LDAP://initials=" & userInitials)

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Author Comment

by:mcnuttlaw
ID: 39645408
Your second suggestion did not work.  Returned "an operations error occurred".

However, your old code looks interesting.  I'll give that a try.
0
 
LVL 9

Accepted Solution

by:
WebDevEM earned 2000 total points
ID: 39645441
Here's a more complete version of that code - what I pasted above was just the SELECT string.
LDAPServer="ServerNameDomainController"
SQLStmt = "SELECT ipphone, homephone, mobile, name, mail, telephonenumber, givenName, sn, userAccountControl, extensionattribute1,extensionattribute2 FROM 'LDAP://" & LDAPServer & "' WHERE objectClass='*' AND ObjectCategory='user' ORDER BY sn"

		Set Conn = CreateObject("ADODB.Connection")
		Conn.Provider = "ADSDSOObject"
		Conn.Properties("User ID") = "userid"
		Conn.Properties("Password") = "password"
		Conn.Properties("Encrypt Password") = True


		Conn.Open "ADs Provider"

		Set rs = Conn.Execute(SQLStmt)

Open in new window

Then you loop through the rs and display the fields like this:
Do While Not rs.EOF Or rs.BOF
  Response.Write("Phone - " & rs("telephonenumber") & "<BR>")
  rs.MoveNext
Loop

Open in new window

0
 
LVL 2

Author Comment

by:mcnuttlaw
ID: 39647936
I'm getting an error:

Object required: Response
0
 
LVL 9

Expert Comment

by:WebDevEM
ID: 39647991
Oh, that code was originally in a web page not a standalone VBS file.  What to do instead depends on how you want your output.  If you want it to display on the screen, you can use
WScript.echo(rs("telephonenumber"))

Open in new window

What is it you're trying to do with the results?
0
 
LVL 2

Author Comment

by:mcnuttlaw
ID: 39651231
I'm experimenting with different methods (including yours) of getting info from AD for a project.

I was able to accomplish the task by using my original method via a login script.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article we want to have a look at the directory attributes which are used by Microsoft to store the so called Security Identifiers (SID). These SIDs plays an important role in delegating and granting permissions and in authentication of trus…
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question