473,566 Members | 2,772 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Querying Active Directory for a specific user

Seth Schrock
2,965 Recognized Expert Specialist
I have found the following thread which ADezii solved very nicely: ...Grab information from AD.... However, this just pulls for the current user. I want to be able to specify the user so that I can get their email address. From research that I have done online (ADSI Active Directory), you must use LDAP and not WinNT to be able to pull the EmailAddress property of the user. Again from looking online, it seems that I need to know the OU where the user is located to be able to do this. The problem is that our users are arranged through many different OUs and I can't predict where via code.

What I'm trying to do is get the email addresses for everyone in a specific security group. I had managed to get several pieces of information using the following code.
Expand|Select|Wrap|Line Numbers
  1. Public Sub UsersInGroup(GroupName As String)
  2. Dim adGroup As ActiveDs.IADsGroup
  3. Dim adMembers As IADsMembers
  4. Dim adMember As IADs
  5. Dim adNTUser As IADsUser
  6.  
  7. Set adGroup = GetObject("WinNT://MyDomain/" & GroupName)
  8. Set adMembers = adGroup.Members
  9.  
  10. For Each adMember In adMembers
  11.  
  12.     Set adNTUser = GetObject("WinNT://MyDomain/" & adMember.Name & ",user")
  13.     Debug.Print adMember.Name, adNTUser.FullName, adNTUser.IsAccountLocked, adNTUser.PasswordExpirationDate, adNTUser.EmailAddress
  14.  
  15. Next
  16.  
  17. End Sub
  18.  
Everything in this works except for the adNTUser.EmailA ddress at the end of the Debug.Print line. So I just need to replace the WinNT string with an LDAP string that allows me to search for the user by username. I had tried
Expand|Select|Wrap|Line Numbers
  1. "LDAP://CN=" & adNTUser.FullName & ",CN=Users,DC=MyDomain,DC=com"
but I get an error saying "There is no such object on the server". I don't know much about LDAP and I have no clue what else to try.
Oct 28 '14 #1
10 12866
iam_clint
1,208 Recognized Expert Top Contributor
I do believe you are looking for username and not FullName for the LDAP query.
Oct 28 '14 #2
Seth Schrock
2,965 Recognized Expert Specialist
Well, based on the solution ADezii posted in the thread that I referenced, the string that sysInfo.UserNam e uses the user's full name along with the other elements I listed. So while you are accurate that the property used is called UserName, it refers to the user's name and not the computer username that I'm looking for.
Oct 28 '14 #3
Rabbit
12,516 Recognized Expert Moderator MVP
This is a VBScript that I use at work to grab AD info quickly given their first and last name. The part you'll be interested in is the filter that is used to find the correct AD object regardless of which OU they are in.

Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2.  
  3. Dim objRoot, strDomain, objConn, objComm, objRecordset
  4. Dim sFilter, sAttribs, sDepth, sBase, sQuery
  5.  
  6. Set objRoot = GetObject("LDAP://RootDSE")
  7. strDomain = objRoot.Get("DefaultNamingContext")
  8. Set objConn = CreateObject("ADODB.Connection")
  9. Set objComm = CreateObject("ADODB.Command")
  10.  
  11. sFilter = "(&(objectClass=person)(sn=" & InputBox("Enter Last Name") & ")(givenName=" & InputBox("Enter First Name") & "))"
  12. sAttribs = "adsPath,objectCategory,physicalDeliveryOfficeName,telephoneNumber,streetAddress,manager,mail,sAMAccountName"
  13. sDepth = "SubTree"
  14. sBase = "<LDAP://" & strDomain & ">"
  15. sQuery = sBase & ";" & sFilter & ";" & sAttribs & ";" & sDepth
  16.  
  17. objConn.Open "Data Source=Active Directory Provider;Provider=ADsDSOObject"
  18. Set objComm.ActiveConnection = objConn
  19. objComm.Properties("Page Size") = 10000
  20. objComm.CommandText = sQuery
  21. Set objRecordset = objComm.Execute
  22.  
  23. Do Until objRecordset.EOF
  24.     MsgBox objRecordset("adsPath") & vbCrLf & vbCrLf & "Account Name: " & Nz(objRecordset("sAMAccountName"), "") & vbCrLf & "Street Address: " & Nz(objRecordset("streetAddress"), "") & vbCrLf & "Office: " & Nz(objRecordset("physicalDeliveryOfficeName"), "") & vbCrLf & "Telephone Number: " & Nz(objRecordset("telephoneNumber"), "") & vbCrLf & "E-Mail: " & Nz(objRecordset("mail"), "") & vbCrLf & "Manager: " & Nz(objRecordset("manager"), "")
  25.     objRecordset.MoveNext
  26. Loop
  27.  
  28. Function Nz(strItem, strReturn)
  29.     If IsNull(strItem) Then
  30.         Nz = strReturn
  31.     Else
  32.         Nz = strItem
  33.     End If
  34. End Function
Oct 28 '14 #4
Seth Schrock
2,965 Recognized Expert Specialist
Can I filter based on the username? For some strange reason, I have access to the FullName property through WinNT, but not the FirstName and LastName properties. I get the username through the Member object.
Oct 28 '14 #5
Rabbit
12,516 Recognized Expert Moderator MVP
You can filter by any field available in AD. I believe the username field is called sAMAccountName. Which may or may not include the domain as part of it. I can't remember of the top off my head.
Oct 29 '14 #6
Seth Schrock
2,965 Recognized Expert Specialist
It worked. Thanks Rabbit. Here is my completed code that allows me to specify the security group and then outputs the username and email address associated with the user:
Expand|Select|Wrap|Line Numbers
  1. Public Sub UsersInGroup(GroupName As String)
  2. Dim adGroup As ActiveDs.IADsGroup
  3. Dim adMembers As IADsMembers
  4. Dim adMember As IADs
  5. Dim adUser As IADsUser
  6. Dim objConn As Object
  7. Dim objComm As Object
  8. Dim objRecordset As Object
  9. Dim sFilter As String
  10. Dim sAttribs As String
  11. Dim sDepth As String
  12. Dim sBase As String
  13. Dim sQuery As String
  14.  
  15. Set objConn = CreateObject("ADODB.Connection")
  16. Set objComm = CreateObject("ADODB.Command")
  17. Set adGroup = GetObject("WinNT://ftc/" & GroupName)
  18. Set adMembers = adGroup.Members
  19.  
  20. objConn.Open "Data Source=Active Directory Provider;Provider=ADsDSOObject"
  21. Set objComm.ActiveConnection = objConn
  22. objComm.Properties("Page Size") = 10000
  23.  
  24. sAttribs = "adsPath,objectCategory,physicalDeliveryOfficeName,telephoneNumber,streetAddress,manager,mail,sAMAccountName"
  25. sDepth = "SubTree"
  26. sBase = "<LDAP://DC=FTC,DC=com>"
  27.  
  28. For Each adMember In adMembers
  29.     sFilter = "(&(objectClass=person)(sAMAccountName=" & adMember.Name & "))"
  30.     sQuery = sBase & ";" & sFilter & ";" & sAttribs & ";" & sDepth
  31.     objComm.CommandText = sQuery
  32.     Set objRecordset = objComm.Execute
  33.  
  34.     Debug.Print "Account Name: " & Nz(objRecordset("sAMAccountName"), ""), "E-Mail: " & Nz(objRecordset("mail"), "")
  35.  
  36. Next
  37.  
  38. Set objComm = Nothing
  39. Set objConn = Nothing
  40. Set adGroup = Nothing
  41. Set adMembers = Nothing
  42. Set objRecordset = Nothing
  43.  
  44. End Sub
Oct 29 '14 #7
Rabbit
12,516 Recognized Expert Moderator MVP
No problem Seth. By the way, the sAttribs is a listing of fields to return. You don't need to return all the fields that I returned in my script if all you need is the email.
Oct 29 '14 #8
Seth Schrock
2,965 Recognized Expert Specialist
I had noticed that, but then forgot to look into it. Thanks again.
Oct 29 '14 #9
Nauticalgent
100 New Member
A little late, but from within the VBE, which Reference must be enabled to use this?
Mar 22 '19 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

0
1010
by: Trey Mitchell | last post by:
I have built a single class in vb.Net to handle all active directory authentication needed in my web app. People can sign into the website with no problem. There is also a second form of authentication that users can have to access the website. When they use the second form (uses db backend), I see if I know their AD username (stored in...
0
989
by: Brian Mitchell | last post by:
Is there any way to return active directory objects from the object's guid? My basic problem is that I don't know the appropriate search filters. Does anyone know of any sites that list them? (like sn searches for surname) Thanks in advance!!
0
983
by: Gary | last post by:
I'm creating an Infopath form and I need to query Active Directory to obtain some data. Here is the code I'm using (obtained from sample code from MSDN) Private Shared domainADsPath As String = "LDAP://xxx.com" Private Shared username As String = "gary" Private Shared password As String = "password" Private Shared schemaClassNameToSearch As...
0
1153
by: hellosibba | last post by:
i am trying to enumerate and query the organization Domain controller to get all the domains and its corresponding users in it. i also want to query the user's information like full name, designation, contact number.. whatever is being assigned for that user in the domain. i am using vb2005 and the following namespace Imports...
4
10885
by: cpajoe2001 | last post by:
I am not sure if this is the proper place to put this thread but I have run into a road block. I am writing a web app that will create groups in Active Directory then add users to those groups. I have it creating the groups just fine however when i go to add a user i get. The requested operation did not satisfy one or more constraints associated...
3
2801
by: Brian McCullough | last post by:
Hello, I am trying to query ADAM using the ActiveDirectoryMembershipProvider in my ASP.NET 2.0 application, but have been unsuccessful. I have followed the steps in these blog posts, but still am not able to query ADAM. Every time I make a call to login, using the Membership.ValidateUser method, I keep getting a false value. I have tried...
0
1087
by: gdltec | last post by:
I have an ASP page that needs to be populated with employee data from Active Directory (i.e. Name, email, manager, etc), can anyone help me out on how to acomplish this? I just need to know how to query AD from classic ASP. thanks,
0
4151
by: justintaitt | last post by:
Hi, I am new to active directory and am trying to query it for the user name and ID of members of a group to populate a drop down list in the form: firstname lastname (userID) When I run the page which calls this function through visual studio it works fine and the drop down list is populated. However when I run the page over the intranet I get...
4
5245
by: jllg2000 | last post by:
I got this query: CREATE VIEW viewADContacts AS SELECT , SN , ST State FROM OPENQUERY( ADSI2, 'SELECT Name, SN, ST FROM ''LDAP://presidencia.local/CN=Users, DC=domainname,DC=local''') GO
0
1067
by: VBDevo | last post by:
Hello, Can anyone help me in this please I'm trying to restore an Active Directory deleted user, I found the code only in c++ in the MSDN, and it uses the DLL : wldap32.dll , which is under system32 in windows , but when I try to add reference it gives me an error that the assembly is not valid. would you tell me how should I use it , or how...
0
7666
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8108
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7644
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7951
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5213
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3643
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2083
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
925
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.