Announcement

Collapse
No announcement yet.

How to get SMTP addresses from AD

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to get SMTP addresses from AD

    I had found this script on these forums from here at <it won't let me post the URL>

    It helped me quite a bit but I wanted to just add something else to it which might help others that where looking for the same thing.

    I needed all of the SMTP addresses from AD and I needed all of the aliases as well but mark which were the primary and secondary. I am doing a large email migration and I needed to know all of this. As I said before I found the script I used from the forums here but I modified it a little bit to suit my needs and wanted to repost it here just in case anyone else searching google might find it.

    Code:
    'Global variables
    Dim Container
    Dim OutPutFile
    Dim FileSystem
    
    'Initialize global variables
    Set FileSystem = WScript.CreateObject("Scripting.FileSystemObject")
    Set OutPutFile = FileSystem.CreateTextFile("smtpaddresses.txt", True)
    
    'Replace with valid DN of the container you want to enumerate
    Set Container = GetObject("LDAP://DC=domain,DC=com")
    
    'Enumerate Container
    EnumerateUsers Container
    
    'Clean up
    OutPutFile.Close
    Set FileSystem = Nothing
    Set Container = Nothing
    
    '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)
    
         'If you find Users
         Case "user"
         'Select all proxyAddresses
         Dim Alias
         If Not IsEmpty(User.proxyAddresses) Then
           'OutPutFile.Write User.DisplayName
           For Each Alias in User.proxyAddresses
             If InStr(1,Alias,"SMTP",0) > 0 Then
               OutPutFile.WriteLine "Primary" &vbtab &User.DisplayName & vbTab & Replace(Alias,"SMTP:","",1,1,1)
               WScript.Echo "PRIMARY:"&User.DisplayName & vbTab & Replace(Alias,"SMTP:","",1,1,1)
               OutPutFile.WriteLine "Secondary" &vbTab &User.DisplayName & vbTab & Replace(Alias,"smtp:","",1,1,1)
               WScript.Echo "SECONDARY:"&User.DisplayName & vbTab & Replace(Alias,"smtp:","",1,1,1)
             End If
     
             'WScript.Echo Alias
           Next
         End If
    
         Case "organizationalunit" , "container"
         EnumerateUsers User
    
         End Select
       Next
    End Sub
    Last edited by Rems; 3rd March 2011, 14:50.

  • #2
    Re: How to get SMTP addresses from AD

    Thanks Blackice, submissions like this are much appreciated.

    Another way would be to use powershell or adfind maybe? My skills aren't good enough to start colouring things but maybe this in adfind.

    Code:
    adfind -b "OU=Users Container,dc=domain,dc=local" -csv attr samaccountname givenname sn proxyaddresses > c:\User.csv
    cheers
    Andy

    Please read this before you post:


    Quis custodiet ipsos custodes?

    Comment


    • #3
      Re: How to get SMTP addresses from AD

      Originally posted by blackice View Post

      I had found this script on these forums from here at <it won't let me post the URL>
      It helped me quite a bit but I wanted to just add something else to it which might help others that where looking for the same thing.

      I needed all of the SMTP addresses from AD and I needed all of the aliases as well but mark which were the primary and secondary. I am doing a large email migration and I needed to know all of this.
      As I said before I found the script I used from the forums here but I modified it a little bit to suit my needs and wanted to repost it here just in case anyone else searching google might find it.
      Cheers blackice, much appreciated.

      btw, for posting the url see, http://forums.petri.com/showpost.php...09&postcount=5.


      Two remarks about the script though,
      *) The attribute proxyAddresses, in case is not empty, should not always treated as type "Array" by a script. If it would contain just a single entry the value could in some occasions be (and will be in your script) of type "String", the FOR-Next statement will therefore raise an error!

      *) Your script does not obtain secondery addresses.

      sample: adjusted subroutine
      note, The variables 'bWL' and 'iUCnt' should be DIM'd in the main body of the code
      Code:
      'subroutine: List all Users
      
      Sub EnumerateUsers(ByVal Cont)
         Dim obj
      rem - The variables 'bWL' and  'iUCnt' should be declared globally (!)
      
         'Go through all Users and select them
         For Each obj In Cont : Select Case LCase(obj.Class)
      
           'If you find Users
           Case "user"
             Select Case 1 ' exclude system mailboxes
             Case InStr(1,obj.cn, "FederatedEmail.", 1), _
                  InStr(1,obj.cn, "SystemMailbox{", 1), _
                  InStr(1,obj.cn, "DiscoverySearchMailbox", 1)
      
             Case Else ' Select proxyAddresses
             If Not IsEmpty(obj.proxyAddresses) Then
               Dim a, sDisplayName, sUser, Wr
               If bWL = True Then Wr = True : bWL = False
               sDisplayName = obj.DisplayName
               iUCnt = iUCnt + 1
      
               For Each a in obj.GetEx("proxyAddresses")
                 If InStr(1,a,"SMTP:",1) = 1 Then
                   bWL = True
                   If Wr = True Then OutPutFile.WriteLine : Wr = False
                   If Not sDisplayName = sUser Then
                     sUser = sDisplayName
                     OutPutFile.WriteLine mid(1e4+iUCnt,2) & vbNewLine & sUser
                   End If
      
                   If InStr(1,a,"smtp:",0) = 1 Then
                     OutPutFile.WriteLine "   secondary: " & Mid(a,6)
                   Else
                     OutPutFile.WriteLine "   Primary  : " & Mid(a,6)
                   End If
                 End If
               Next
             End If
             End Select
      
           Case "organizationalunit" , "container"
             EnumerateUsers obj
      
         End Select : Next
      End Sub
      Similar threads about collecting e-mail addresses by script can be find via Search Forums using the keyword: "proxyAddresses"


      \Rems
      Last edited by Rems; 3rd March 2011, 19:33.

      This posting is provided "AS IS" with no warranties, and confers no rights.

      __________________

      ** Remember to give credit where credit's due **
      and leave Reputation Points for meaningful posts

      Comment

      Working...
      X