Announcement

Collapse
No announcement yet.

this script exports all smtp address, I only want the primary.

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

  • this script exports all smtp address, I only want the primary.

    Hi,

    I'm new to this forum and I'm quite new at vbscripting. I'm trying to export to a text file, the primary smtp address of all mail enabled users, but this script exports ALL smtp addresses. Can someone help with editing this to only pull the primary?

    Here's the script:



    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=local")
    
    '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",1) > 0 Then
    	OutPutFile.WriteLine User.DisplayName & vbTab & Replace(Alias,"smtp:","",1,1,1)
    			'WScript.Echo 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


    Any help at all would be appreciated.

    Jgoodine
    Last edited by Dumber; 5th November 2007, 23:18. Reason: Wrapped code tags arround the script for easy reading.

  • #2
    Re: this script exports all smtp address, I only want the primary.

    Not vbs, but it does give you the same result though;
    http://forums.petri.com/showthread.php?t=18464

    \Rems

    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


    • #3
      Re: this script exports all smtp address, I only want the primary.

      Hi REMS,

      thanks for the reply. I actually just got it working a few minutes ago. I read up on the intstr command and changed 1 line of code and it gave me all the primary addresses.

      The code change was:

      from:

      For Each Alias in User.proxyAddresses
      If InStr(1,Alias,"smtp",1) > 0 Then

      To:

      For Each Alias in User.proxyAddresses
      If InStr(1,Alias,"SMTP",0) > 0 Then

      Thanks!

      Jgoodine

      Comment


      • #4
        Re: this script exports all smtp address, I only want the primary.

        The attribute 'proxyAddresses' can be empty, singlevalued or multivalued.
        You use in the script *only* a For-Each loop to go through the existing values of this attribute. This can raise an error in cases when this userobject's attribute contain less than two values because your script is missing that check and an alternative string comparing.

        If you are *just* interested in the users primary email addres, use the attribute 'mail' instead. This attribute cannot be multivalued so it is easyer to read with a script.

        I notice that you try to use recursion in the subroutine to enumerate and then search also in each sub-OU's. But that part of the code is to my knowledge not complete.
        Btw.. I have never seen this method to query OUs/Containers and its sub-OU's before, this method opens a lot of bindings the same time. It will make the script run more efficient when you use a script Data Object (ADO) interface instead to search in AD.

        The example script below uses the 'mail' attribute and not 'proxyAddresses'.
        Recursion is fixed, although I don't recommend this method for searching in AD.
        I choose not to write directly to the output file, instead in this script an array is filled with the results. And after the search, the array will be sorted first before writing the results to the file.

        Code:
        '  http://forums.petri.com/showthread.php?t=19411  
        '  modified by: Remco Simons [NL] 2007
        '
        'note: 
        ' This script is still using Recursion to search for certain objects in the AD, 
        ' this is however *not* a recommended method to query also child OU's !!
        ' (use a LDAP search with ADO instead, example:
        '   forums.petri.com/showpost.php?p=78460&postcount=11 ) 
        
        
        'Global variables
        Dim Container
        Dim OutPutFile
        Dim FileSystem
        Dim arrUnsorted
        
        'Initialize global variables
        Set FileSystem = WScript.CreateObject("Scripting.FileSystemObject")
        Set OutPutFile = FileSystem.CreateTextFile("d:\smtpaddresses.txt", True)
        
        Set objRootDSE = GetObject("LDAP://rootDSE")
        strDNSDomain = objRootDSE.Get("defaultNamingContext")
        
        Dim arrNames() : intSize = 0
        col1 = vbEmpty : col2 =vbEmpty
        
        Set Container=GetObject("LDAP://" & strDNSDomain)
        
        EnumerateUsers Container
        
        'Sort output
        Dim arrLines() : intSize = 0
        For i = LBound(arrNames) to Ubound(arrNames) STEP 3
          scanonicalName = arrNames(i)
          sDisplayName = arrNames(i+1)
          sEmail = arrNames(i+2)
        
          intSpaces1 = col1 - Len(sCanonicalName)
          intSpaces2 = col2 - Len(sDisplayName)
        
          ReDim Preserve arrLines(intSize)
          arrLines(intSize) = scanonicalName & space(intSpaces1) & sDisplayName & space(intSpaces2) & sEmail
           intSize = intSize + 1
        Next
        strLines = SortIt(arrLines)
        
        'write file
        OutPutFile.WriteLine join(arrLines, vbNewLine)
        
        'Clean up
        OutPutFile.Close
        Set FileSystem = Nothing
        Set Container = Nothing
        
        'Say Finished when your done
        WScript.Echo "Finished"
        
        WScript.Quit(0)
        
        
        Sub EnumerateUsers(Cont)
          Dim User
          'Go through all Users and select them
          For Each objItem In Cont
            Select Case LCase(objItem.Class)
               'If you find Users
        
               Case "user"
                  If Not IsEmpty(objItem.mail) Then
                    objItem.GetInfoEx Array("canonicalName"), 0
                    strObjCN = objItem.Get("canonicalName")
                    strObjCN = Left(strObjCN,InStrRev(strObjCN, "/")) & "*"
                   ReDim Preserve arrNames(intSize)
                    arrNames(intSize) = strObjCN
                    If col1 =< Len(arrNames(intSize)) Then col1 = Len(arrNames(intSize)) +1
                    intSize = intSize + 1
                   ReDim Preserve arrNames(intSize)
                    arrNames(intSize) = objItem.Displayname
                    If col2 =< Len(arrNames(intSize)) Then col2 = Len(arrNames(intSize)) + 1
                    intSize = intSize + 1
                   ReDim Preserve arrNames(intSize)
                    arrNames(intSize) = objItem.mail
                    intSize = intSize + 1
                  End If
        
               Case "organizationalunit" , "container"
                  'Recursion
                  Set Container2=GetObject("LDAP://" & objItem.distinguishedname)
                  EnumerateUsers Container2
            End Select
          Next
        End Sub
        
        
        Function SortIt(arrUnsorted)
          Dim arrSorted
          Dim bolSorted
          Dim strSorted
          bolSorted = False
          Do Until bolSorted
            bolSorted = True
            For I = 0 to UBound(arrUnsorted)
              ' Compare this entry to the next entry
              If I < UBound(arrUnsorted) Then
                If arrUnsorted(I+1) < arrUnsorted(i) Then
                  strTemp = arrUnsorted(I+1)
                  arrUnsorted(I+1) = arrUnsorted(I)
                  arrUnsorted(I) = strTemp
                  bolSorted = False
                End If
              End If
            Next
          Loop
          strSorted = ""
          For I = 1 to UBound(arrUnsorted)
            strSorted = strSorted & arrUnsorted(I) & vbCrLf
          Next
          SortIt = strSorted
        End Function

        - - -
        But lets stick to the proxyaddresse attribute:
        To determine if you deal with an array or not, you can use;
        Code:
        
        Primary_EmailAddress     = "SMTP:"
        Secondary_EmailAddresses = "smtp:"
        If IsArray(objItem.proxyAddresses) then
          For Each address in objItem.proxyAddresses
               If CBool(InStr(address, Primary_EmailAddress)) Then  
                      wscript.echo objItem.DisplayName, vbTab, Mid(address,6), vbTab, ", (from Array)" 
               End If
          Next
        ElseIf Not IsEmpty(objItem.proxyAddresses) then
           address = objItem.proxyAddresses
               If CBool(InStr(address, Primary_EmailAddress)) Then
                      wscript.echo objItem.DisplayName, vbTab, Mid(address,6), vbTab, ", (String value)"
               End If
        End If
        Or, you can simply use the GetEx method to retrieve all values of the attribute.
        The information retrieved from GetEx is returned in the form of a variant array. An array is a data structure containing one or more values, and a variant is a data structure containing any data type, such as a string or an integer. Thus a variant array is a data structure containing one or more values of any type.
        Code:
        Sub EnumerateUsers(Cont)
          Dim objItem
          Dim Primary_EmailAddress
          Dim Secondary_EmailAddresses
          'Go through all Users and select them
          For Each objItem In Cont
            Select Case LCase(objItem.Class)
               'If you find Users
        
              Case "user"
               Primary_EmailAddress     = "SMTP:"
               Secondary_EmailAddresses = "smtp:"
               If Not IsEmpty(objItem.proxyAddresses) then
                  For Each address in objItem.GetEx("proxyAddresses")
                    If CBool(InStr(address, Primary_EmailAddress)) Then  
                      wscript.echo objItem.DisplayName, vbTab, Mid(address,6), vbTab, ", (variant Array value)" 
                    End If
                  Next    
               End If
        
              Case "organizationalunit" , "container"
                  'Recursion
                  Set Container2=GetObject("LDAP://" & objItem.distinguishedname)
                  EnumerateUsers Container2
            End Select
          Next
        End Sub
        Instead of using recursion, here is an example how perform a LDAP search using a ADO connection
        http://forums.petri.com/showpost.php...0&postcount=11

        \Rems
        Last edited by Rems; 20th November 2007, 15:19. Reason: typos

        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


        • #5
          Re: this script exports all smtp address, I only want the primary.

          Hi REMS,

          Thank you for the time you put into replying to my post, and thank you for the revised code.

          jgoodine

          Comment

          Working...
          X