Solved

Map Active Directory Logon Account to email address.

Posted on 2013-11-04
7
694 Views
Last Modified: 2013-11-06
Map AD Logon Account to email address.

we have Active directory Logon account as numbers. but email addresses are First initial and last name.

example: our AD domain is = Mydomain.Corp
my logon account  is=111111
and my email address is = john@Mydomain.com

as you notice the AD domain name is different than email domain name.

I have a list of users AD accounts:
111111
222222
333333
444444
555555

I want to send them emails through a script , but I do not know how to map their AD account to their email address through a Script..

Any help will be very much appreciated.

Thank you.
0
Comment
Question by:jskfan
7 Comments
 
LVL 25

Assisted Solution

by:Diverse IT
Diverse IT earned 100 total points
ID: 39623529
Hi jskfan,

Can I ask why you have it setup like that?

What version of Exchange are you running?
0
 
LVL 21

Expert Comment

by:dan_blagut
ID: 39623675
Hi
Is the email address field of AD account OK?

Dan
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 400 total points
ID: 39623895
Hi, see if this script will work to give you the "mail" property of each user account in your input file.

Regards,

Rob.

strInputFile = "Users.txt"
strOutputfile = "Results.csv"

If Right(LCase(WScript.FullName), 11) = "wscript.exe" Then
	Set objShell = CreateObject("WScript.Shell")
	objShell.Run "cscript """ & WScript.ScriptFullName & """", 1, False
	Set objShell = Nothing
	WScript.Quit
End If

Set objFSO = CreateObject("Scripting.FileSystemObject")
Const intForReading = 1

Set objInputFile = objFSO.OpenTextFile(strInputFile, intForReading, False)

Set adoCommand = CreateObject("ADODB.Command")
Set adoConnection = CreateObject("ADODB.Connection")
adoConnection.Provider = "ADsDSOObject"
adoConnection.Open "Active Directory Provider"
adoCommand.ActiveConnection = adoConnection

 ' Search entire Active Directory domain.
Set objRootDSE = GetObject("LDAP://RootDSE")

strDNSDomain = objRootDSE.Get("defaultNamingContext")
strBase = "<LDAP://" & strDNSDomain & ">"

strDetails = """Login Name"",""Email"""

While Not objInputFile.AtEndOfStream
	strNumber = objInputFile.ReadLine
	WScript.Echo "Looking for " & strNumber
	
	strFilter = "(&(objectCategory=person)(objectClass=user)(samAccountName=" & strNumber & "))"
	
	' Comma delimited list of attribute values to retrieve.
	strAttributes = "samAccountName,mail"
	
	' Construct the LDAP syntax query.
	strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
	adoCommand.CommandText = strQuery
	adoCommand.Properties("Page Size") = 100
	adoCommand.Properties("Timeout") = 30
	adoCommand.Properties("Cache Results") = False
	
	' Run the query.
	Set adoRecordset = adoCommand.Execute
	 
	' Enumerate the resulting recordset.
	Do Until adoRecordset.EOF
	    ' Retrieve values and display.
		strUserName = adoRecordset.Fields("samAccountName").Value
		strMail = adoRecordset.Fields("mail").Value
		strDetails = strDetails & VbCrLf & """" & strUserName & """,""" & strMail & """"
	    ' Move to the next record in the recordset.
	    adoRecordset.MoveNext
	Loop
	
	' Clean up.
	adoRecordset.Close
	Set adoRecordset = Nothing
Wend

adoConnection.Close

Set objOutputFile = objFSO.CreateTextFile(strOutputFile, True)
objOutputFile.Write strDetails
objOutputFile.Close
Set objOutputFile = Nothing

WScript.Echo "Done"
MsgBox "Done. Please see " & strOutputFile

Open in new window

0
 
LVL 37

Expert Comment

by:Jamie McKillop
ID: 39624172
What version of Exchange?

-JJ
0
 

Author Closing Comment

by:jskfan
ID: 39628583
Thanks
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

When you have clients or friends from around the world, it becomes a challenge to arrange a meeting or effectively manage your time. This is where Outlook's capability to show 2 time zones in one calendar comes in handy.
This video demonstrates how to sync Microsoft Exchange Public Folders with smartphones using CodeTwo Exchange Sync and Exchange ActiveSync. To learn more about CodeTwo Exchange Sync and download the free trial, go to: http://www.codetwo.com/excha…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

808 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