Become a Columnist Microsoft Exchange Site Microsoft Support SiteMSDN Exchange Site
Subscribe to OutlookExchange
Anderson Patricio
Ann Mc Donough
Bob Spurzem
Brian Veal
Catherine Creary
Cherry Beado
Colin Janssen
Collins Timothy Mutesaria
Drew Nicholson
Fred Volking
Glen Scales
Goran Husman
Guy Thomas
Henrik Walther
Jason Sherry
Jayme Bowers
John Young
Joyce Tang
Justin Braun
Konstantin Zheludev
Kristina Waters
Kuang Zhang
Mahmoud Magdy
Martin Tuip
Michael Dong
Michele Deo
Mitch Tulloch
Nicolas Blank
Pavel Nagaev
Ragnar Harper
Ricardo Silva
Richard Wakeman
Russ Iuliano
Santhosh Hanumanthappa
Shannal L. Thomas
Steve Bryant
Steve Craig
Todd Walker
Tracey J. Rosenblath

 

 
 

Mailbox Crawl script    Download Script

The following script has been written to crawl through every eml object in every folder within a given mailbox. This can be useful in reporting or cleaning of mail stores. For example if its combined with the script in my content filtering article you can use this to produce a report that will provide you with the amount of storage space being used to store specific attachment types. Or you could use it to monitor or remove any undesirable mailbox content.

How it works

This is just a straight script that is designed to be run from the command line or a batch file that passes the mailbox alias of the mailbox you want to crawl as the parameter for the execution of the script. The front end of the code does just this it allows the passage of the mailbox alias as a parameter of the script then attaches it to form the complete mailbox URL. Note you need to modify this part of the code to include your full mailbox domain name (if you not sure what it is have a look at the M: drive on your server) note this script is designed to be run on the mailbox server this is why the file://./ is used you could also use the http://./ is you wish to run it on a remote workstation.

For I = 0 to objArgs.Count - 1
   if I = 0 then
            inbstr = objArgs(I)
   else
            inbstr = inbstr & " " & objArgs(I)
   end if
Next
inbstr = "file://./backofficestorage/AD.man.net.au/MBX/" & inbstr

The next part of the code setups up and opens an ADO record set that contains all the folders in the information store. It does this using a select query with the deep transverse option. Deep transverse queries are very resource intensive queries on a server and should be used sparingly. This is because they are a recursive query that will scan though for every object in the mailbox that matches the search criteria. For this reason I haven’t used this type of query to get access to every object in the mailbox store; doing so can have a very detrimental affect on the server resources in regards to processor and memory when querying large mailboxes. 

set WshShell = CreateObject("WScript.Shell")
Set Conn = CreateObject("ADODB.Connection")
Set fso = CreateObject("Scripting.FileSystemObject")
set wfile =  fso.opentextfile("c:\temp\mbox.csv",2,true)
Set Rec = CreateObject("ADODB.Record")
Set Rs = CreateObject("ADODB.Recordset")
set Rec1 = CreateObject("ADODB.Record")
Set Rs1 = CreateObject("ADODB.Recordset")
Conn.Provider = "ExOLEDB.DataSource"
Rec.Open inbstr
strView = "SELECT ""DAV:href"""
strView = strView & "FROM scope ('deep traversal of """& inbstr & """') "strview = strview & "WHERE ""DAV:isfolder"" = true"rs.CursorLocation = 3 'adUseServer = 2, adUseClient = 3
rs.CursorType = 3
Rs.Open strView, Rec.ActiveConnection, 2

Once this record set is open the next part of the code creates another record set that contains all the items in the folder in the current record in the first record set. This time the query does a shallow transversal meaning that it only selects the items in that folder and doesn’t go any deeper.

If Rs.RecordCount <> 0 Then
                Rs.MoveFirst
                While Not Rs.EOF
                                workfolderRef = inbstr & "/inbox"
                                workfolder = Rs.fields("DAV:href").value
                                Rec1.open workfolderRef
                                strView = "SELECT ""DAV:displayname"",""DAV:href"""
                                strView = strView & "FROM scope ('shallow traversal of """& workfolder & """') "
                                strview = strview & " WHERE ""DAV:isfolder"" = false AND ""DAV:ishidden"" = false"
                                rs1.CursorLocation = 3 'adUseServer = 2, adUseClient = 3
                                rs1.CursorType = 3
                                rs1.Open strView, Rec1.ActiveConnection, 1
                                if rs1.Recordcount <> 0 Then

 This part of the code is where you can put your own processing section as a very simple example I put some code that writes the name of the mail URL to a log file.

Rs1.Movefirst
                                    While Not Rs1.EOF
                                                murl = Rs1.Fields("DAV:href").Value
                                                wfile.write murl & vbCrLf
                                    Rs1.MoveNext
                                    Wend
                        end if

The rest of the code closes down and cleans up all the objects

Running this script

To execute this script you can use a batch file or just execute it from the command line with the mailbox alias as the parameter eg mcraw1.vbs mailboxalias

Extending this script

This code has a lot of scope for extension by extending the second select query with the required parameters you can then extend the logging or other processing you want to do within this code. As an example of this have a look at this article where I’ve combined this script with a content filter script to produce a simple content scanning script for an mailbox store.  

Download Script