Solved

Map Active Directory Logon Account to email address.

Posted on 2013-11-04
7
695 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
[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
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 22

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

A company’s centralized system that manages user data, security, and distributed resources is often a focus of criminal attention. Active Directory (AD) is no exception. In truth, it’s even more likely to be targeted due to the number of companies …
Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
This video discusses moving either the default database or any database to a new volume.
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…

756 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