Richard
Mon Apr 28 07:48:00 CDT 2008
Sumit wrote:
> I need to retrieve the email address(abc@xyz.com) from AD based on
> first name and lastname.
> Actually i will be getting a excel file with firstname and a lastname
> of users and i have to fetch the corresponding email addresses and
> write back in the excel sheet.
>
> Could anyone help me with code?
It would be easier to write a program to retrieve first name, last name,
common name, and email address for all users in the domain and write that to
a spreadsheet. What you ask can be done, but the first and last name fields
are not required. Even if they are given values, they may not uniquely
identify the users. If an attempt were made we would need to know if the
first and last name fields are populated with values in ADUC for all users,
or do you mean to construct the Common Name's of the users from the first
and last names. If the latter, are the names in the form "First Last", or
"Last, First"? And Common Names may not uniquely identify the users in the
domain.
I have an example program that documents all users in the domain in a csv
file linked here:
http://www.rlmueller.net/DocumentUsers.htm
First and Last names are documented, but not the email address. The
attribute you want (if you do not use Exchange) is the "mail" attribute. The
program can be modified to document that attribute as well (it is a single
valued string).
If you need code to read your first and last names and find the user, I have
an example program that finds objects given their "Common Name" linked here:
http://www.rlmueller.net/Search%20for%20Common%20Name.htm
You would need to contruct the value of strCN from the first and last names
read from the spreadsheet. If instead of Common Name, you find users by
givenName (first name) and sn (last name) attributes, the filter would be
changed from:
strFilter = "(cn=" & strCN & ")"
To something like this (where strFirst and strLast are read from your
spreadsheet):
strFirst = "Jim"
strLast = "Smith"
strFilter = "(&(givenName=" & strFirst & ")(sn=" & strLast & "))"
You would need to repeat this search for each user. This is in addition to
the code to read the spreadsheet and write the value of the mail attribute.
--
Richard Mueller
Microsoft MVP Scripting and ADSI
Hilltop Lab -
http://www.rlmueller.net
--