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. - Public Sub UsersInGroup(GroupName As String)
-
Dim adGroup As ActiveDs.IADsGroup
-
Dim adMembers As IADsMembers
-
Dim adMember As IADs
-
Dim adNTUser As IADsUser
-
-
Set adGroup = GetObject("WinNT://MyDomain/" & GroupName)
-
Set adMembers = adGroup.Members
-
-
For Each adMember In adMembers
-
-
Set adNTUser = GetObject("WinNT://MyDomain/" & adMember.Name & ",user")
-
Debug.Print adMember.Name, adNTUser.FullName, adNTUser.IsAccountLocked, adNTUser.PasswordExpirationDate, adNTUser.EmailAddress
-
-
Next
-
-
End Sub
-
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 - "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.
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.
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.
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. - Option Explicit
-
-
Dim objRoot, strDomain, objConn, objComm, objRecordset
-
Dim sFilter, sAttribs, sDepth, sBase, sQuery
-
-
Set objRoot = GetObject("LDAP://RootDSE")
-
strDomain = objRoot.Get("DefaultNamingContext")
-
Set objConn = CreateObject("ADODB.Connection")
-
Set objComm = CreateObject("ADODB.Command")
-
-
sFilter = "(&(objectClass=person)(sn=" & InputBox("Enter Last Name") & ")(givenName=" & InputBox("Enter First Name") & "))"
-
sAttribs = "adsPath,objectCategory,physicalDeliveryOfficeName,telephoneNumber,streetAddress,manager,mail,sAMAccountName"
-
sDepth = "SubTree"
-
sBase = "<LDAP://" & strDomain & ">"
-
sQuery = sBase & ";" & sFilter & ";" & sAttribs & ";" & sDepth
-
-
objConn.Open "Data Source=Active Directory Provider;Provider=ADsDSOObject"
-
Set objComm.ActiveConnection = objConn
-
objComm.Properties("Page Size") = 10000
-
objComm.CommandText = sQuery
-
Set objRecordset = objComm.Execute
-
-
Do Until objRecordset.EOF
-
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"), "")
-
objRecordset.MoveNext
-
Loop
-
-
Function Nz(strItem, strReturn)
-
If IsNull(strItem) Then
-
Nz = strReturn
-
Else
-
Nz = strItem
-
End If
-
End Function
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.
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.
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: - Public Sub UsersInGroup(GroupName As String)
-
Dim adGroup As ActiveDs.IADsGroup
-
Dim adMembers As IADsMembers
-
Dim adMember As IADs
-
Dim adUser As IADsUser
-
Dim objConn As Object
-
Dim objComm As Object
-
Dim objRecordset As Object
-
Dim sFilter As String
-
Dim sAttribs As String
-
Dim sDepth As String
-
Dim sBase As String
-
Dim sQuery As String
-
-
Set objConn = CreateObject("ADODB.Connection")
-
Set objComm = CreateObject("ADODB.Command")
-
Set adGroup = GetObject("WinNT://ftc/" & GroupName)
-
Set adMembers = adGroup.Members
-
-
objConn.Open "Data Source=Active Directory Provider;Provider=ADsDSOObject"
-
Set objComm.ActiveConnection = objConn
-
objComm.Properties("Page Size") = 10000
-
-
sAttribs = "adsPath,objectCategory,physicalDeliveryOfficeName,telephoneNumber,streetAddress,manager,mail,sAMAccountName"
-
sDepth = "SubTree"
-
sBase = "<LDAP://DC=FTC,DC=com>"
-
-
For Each adMember In adMembers
-
sFilter = "(&(objectClass=person)(sAMAccountName=" & adMember.Name & "))"
-
sQuery = sBase & ";" & sFilter & ";" & sAttribs & ";" & sDepth
-
objComm.CommandText = sQuery
-
Set objRecordset = objComm.Execute
-
-
Debug.Print "Account Name: " & Nz(objRecordset("sAMAccountName"), ""), "E-Mail: " & Nz(objRecordset("mail"), "")
-
-
Next
-
-
Set objComm = Nothing
-
Set objConn = Nothing
-
Set adGroup = Nothing
-
Set adMembers = Nothing
-
Set objRecordset = Nothing
-
-
End Sub
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.
I had noticed that, but then forgot to look into it. Thanks again.
A little late, but from within the VBE, which Reference must be enabled to use this?
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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!!
|
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...
|
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...
|
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...
| |
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...
|
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,
|
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...
|
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
|
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...
|
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...
| |
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...
|
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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
|
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...
| |