?
Solved

Query Active Director based on prompted username

Posted on 2013-11-13
8
Medium Priority
?
264 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
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.

 
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

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!

Question has a verified solution.

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

Recently I finished a vbscript that I thought I'd share.  It uses a text file with a list of server names to loop through and get various status reports, then writes them all into an Excel file.  Originally it was put together for our Altiris server…
This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
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…
Suggested Courses

762 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