Solved

Map Active Directory Logon Account to email address.

Posted on 2013-11-04
7
696 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
Always backup Domain, SYSVOL etc.using processes according to Microsoft Best Practices. This is meant as a disaster recovery process for small environments that did not implement backup processes and did not run a secondary domain controller that ne…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

734 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