Locate smtp addresses and path

bachmannit
bachmannit used Ask the Experts™
on
I've been tasked with producing a way of locating email addresses in our domain, I have tried using the script below which pruduces the smtp addresses but not the location i.e. mailbox or public folder.
Could someone ammend the script to show the location please.
The Script
Option Explicit

' Global Constants

Const ADS_SCOPE_SUBTREE = 2

' Global Variables

Dim objFileSystem, objFile, objRootDSE
Dim strDomainName
StrDomainName="D Name"
'
' Subroutines
'

Sub FindAddress

      ' Runs a simple AD Query to find proxyAddresses.

      Dim objConnection, objCommand, objRecordSet
      Dim strAddress
      Dim arrAddresses

      Set objConnection = CreateObject("ADODB.Connection")
      objConnection.Provider = "ADsDSOObject"
      objConnection.Open "Active Directory Provider"

      Set objCommand = CreateObject("ADODB.Command")
      objCommand.ActiveConnection = objConnection

      objCommand.CommandText = "SELECT proxyAddresses FROM 'LDAP://" & strDomainName & "'"

      objCommand.Properties("Page Size") = 1000
      objCommand.Properties("Timeout") = 600
      objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
      objCommand.Properties("Cache Results") = False

      Set objRecordSet = objCommand.Execute

      While Not objRecordSet.EOF
            On Error Resume Next
            arrAddresses = objRecordSet.Fields("proxyAddresses")
            For Each strAddress In arrAddresses
                  If InStr(1, strAddress, "smtp:", VbTextCompare) Then
                        objFile.WriteLine Replace(LCase(strAddress), "smtp:", "")
                  End If
            Next
            On Error Goto 0
            objRecordSet.MoveNext
      Wend

      objConnection.Close

      Set objRecordSet = Nothing
      Set objCommand = Nothing
      Set objConnection = Nothing
End Sub

'
' Main Code
'

Set objFileSystem = CreateObject("Scripting.FileSystemObject")
Set objFile = objFileSystem.OpenTextFile("Addresses1.txt", 2, True, 0)

Set objRootDSE = GetObject("LDAP://RootDSE")
strDomainName = objRootDSE.Get("defaultNamingContext")
WScript.Echo "Searching Domain: " & strDomainName & VbCrLf

FindAddress

Set objFile = Nothing
Set objFileSystem = Nothing
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Try this:

Change the string that says
  "SELECT proxyAddresses FROM 'LDAP://" & strDomainName & "'"
to
  "SELECT proxyAddresses,objectClass FROM 'LDAP://" & strDomainName & "'"

And insert this line
  objFile.WriteLine objRecordSet.Fields("objectClass")
just before the line that says
  On Error Goto 0

Author

Commented:
To LeeDerbyshire
I sorry my friend but your suggestion doesn't work it either fails to get the data or fails to print it to file, do you have anything further I could try please.
I see - the objectClass is an array.  Try this While ... Wend block instead of the original one:

  While Not objRecordSet.EOF
    On Error Resume Next
    arrAddresses = objRecordSet.Fields("proxyAddresses")
    For Each strAddress In arrAddresses
      If InStr(1, strAddress, "smtp:", VbTextCompare) Then
        objFile.WriteLine Replace(LCase(strAddress), "smtp:", "")
        Dim arrObjectClasses, objectClass
        arrObjectClasses = objRecordSet.Fields("objectClass")
        For Each objectClass In arrObjectClasses
          If objectClass = "person" Or objectClass = "publicFolder" Then
            objFile.WriteLine objectClass
          End If
        Next
      End If
    Next
    On Error Goto 0
    objRecordSet.MoveNext
  Wend
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
To LeeDerbyshire,
Your last solution is good thanks very much, just one further requirement can you give me a line to extract and print the public folder path because at the moment we have the smtp address , the name of the folder and whether it's a user or public folder which is all useful but the file path to the public folder would be a great help
Many Thanks
Not sure what you mean by the file path.  The folders are all in a single file named pub.edb .

Author

Commented:
Ok I'll try to clarify, For example in our public folders we have a path to a particular folder where the smtp address resides and that is where exchange delivers the incoming mail. It could be 2 or 3 levels in i.e. in exchange system manager it would be under Folders/Public Folders/Departments/"dept name"/"company name"
It's that path I'm after which would enable easy location of the mail address
Hope this is a better explanation
Hmm.  There doesn't seem to be anything in AD that gives the folder path like that.  I used ADSI Edit to look at a few mail-enabled folders here, and the path doesn't seem to be displayed anywhere.  You'd need an actual Exchange API (LDAP isn't one, as such) to get the folder path, but then you wouldn't be able to get the SMTP address.

Author

Commented:
Thank-you for time and help it is much appreciated

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial