Tag Archive for 'Linux'

Script to Extract Email Addresses from AD

I had needed to setup a unix based mail gateway to relay mail through to Exchange on one of my networks.

I had found an article on how to do this The script below reflects a modification of a script I found Mail Relay/; however, it needed some adjustment.

I made some modifications below to handle contacts and groups.

‘ Export all valid recipients (= proxyAddresses) into a
‘ file virtual.txt

‘ Ferdinand Hoffmann & Patrick Koetter
‘ 20021100901
‘ Shamelessly stolen from
‘ http://www.microsoft.com/windows2000/techinfo/ \
‘ planning/activedirectory/bulksteps.asp

‘Global variables
Dim Container
Dim OutPutFile
Dim FileSystem

‘Initialize global variables
Set FileSystem = WScript.CreateObject(“Scripting.FileSystemObject”)
Set OutPutFile = FileSystem.CreateTextFile(“virtual.txt”, True)
Set Container=GetObject(“LDAP://DC=local,DC=p2sol,DC=com”)

‘Enumerate Container
EnumerateUsers Container

‘Clean up
OutPutFile.Close
Set FileSystem = Nothing
Set Container = Nothing
Dim Alias

‘Say Finished when your done
WScript.Echo “Finished”
WScript.Quit(0)

‘List all Users
Sub EnumerateUsers(Cont)
Dim User

‘Go through all Users and select them
For Each User In Cont
Select Case LCase(User.Class)

Case “contact”
If Not IsEmpty(User.Mail) Then
OutPutFile.WriteLine “alias: SMTP ” & User.Mail
End If

‘If you find groups
Case “group”
‘Select all proxyAddresses
If Not IsEmpty(User.proxyAddresses) Then
For Each Alias in User.proxyAddresses
OutPutFile.WriteLine “alias: ” & Alias
‘WScript.Echo Alias
Next
End If

‘If you find Users
Case “user”
‘Select all proxyAddresses
If Not IsEmpty(User.proxyAddresses) Then
For Each Alias in User.proxyAddresses
OutPutFile.WriteLine “alias: ” & Alias
‘WScript.Echo Alias
Next
End If

Case “organizationalunit” , “container”
EnumerateUsers User

End Select
Next
End Sub