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
Steve Bryant
Steve Craig
Todd Walker
Tracey J. Rosenblath
 
 

Mailbox Size Script                            Download Scripts

For the base of the mailbox size script I've used the VBS sample from the following Microsoft Qbase article, This script uses a recursive shallow traversal query of every folder in a mailbox to determine the size of the mailbox. The modifications I've done provide code to log each folder size in a database along with the date information. Lets have a look at the main parts of the code.

Dim obArgs,cArgs,iSize,ndate,tmailbox

Set Cnxn1 = CreateObject("ADODB.Connection")
strCnxn1 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d:\mbsize.mdb;"
Cnxn1.Open strCnxn1
ndate = getdate1()

Set obArgs = WScript.Arguments
tmailbox = obArgs.Item(0)

Main

Sub Main()
Dim sConnString,domainname

rem On Error Resume Next
domainname = "your.domain.com.au"
' Set up connection string to mailbox.
sConnString = "file://./backofficestorage/" & domainname
sConnString = sConnString & "/mbx/" & obArgs.Item(0) & "/NON_IPM_SUBTREE"
WScript.Echo sConnString

iSize = 0

RecurseFolder(sConnString)
sqlstate1 = "insert into dbo_mailboxusage ([date],Mailbox_Name,folder_path,folder_name,folder_size) values ('" & ndate & "','" & tmailbox & "','Totalsize','" & replace(tmailbox,"'","") & "','" & replace(formatnumber((iSize/1024/1024),2),",","") & "')"
Cnxn1.Execute(sqlstate1)
WScript.Echo "Mailbox Size: " & iSize
End Sub

The first section of the code takes the mailbox name as the input parameter and setups and opens a connection to an access database to record the folder size information. I've used a function call getdate1() which serializes the date that I want to insert into the database, I've done this because I find it a lot easier to work with a serial date when it comes to database queries. The next section of code builds the Exoldb URL, the domainname variable which I've highlighted in red needs to be changed to your primary SMTP domain your using in Exchange (if your not sure what it is check the M: drive of your server or have a look at the default recipient policy). The next section of code then calls the recursive folder sub which is responsible for querying each folder and sub folders in the mailbox to work out their size. Once all folders have been recursed though the public isize variable will now contain the total size of the mailbox. The next string inserts this into the database and sets the path to Totalsize this allows for this row to be uniquely queried on.

The RecurseFolder sub (its a bit to large to list) takes the Exoledb URL passed from the main sub and then goes about performing a shallow traversal query of the inbox. One noticeable difference in this query string to the one you may have noticed on other scripts in my column is the past is the addition of the /NON_IPM_SUBTREE parameter to the end of the mailbox string. What this does is that it includes all the system folders in the query as well as all the normal mail folders. In a users mailbox this can include search folders and free/busy information. Normally not a lot of data is stored in these types of folders so its debatable whether its useful to include this data within your mailbox size report or not. The next part of the code grabs the folder size using the http://schemas.microsoft.com/mapi/proptag/x0e080003 mapi property you could also use the http://schemas.microsoft.com/exchange/foldersize property. This data is written to the database along with a serialized date and folder information.

Installing and using the scripts

There are a couple of hard-coded paths in these scripts that you can change if you like but need to be taken into account. The first is in the mailbox query script.

 cmdexe = "c:\winnt\system32\cscript.exe d:\mreport1.vbs " & rs.fields("Samaccountname")

This line points to the mailbox size script and it needs to be located on the root of your d: drive. The other lines that needs to be modified are the sections in red that I mentioned previously including

DomainName = "your.domain.com.au"
strDefaultNamingContext = "DC=your,D=domain,DC=com,DC=au"


These two lines from the mailbox query script refer to your Active Directory domain name not your Exchange SMTP domain name (which may or may not be the same). The last line in the mailbox query script that needs to be modified is the msExchHomeServerName in the GalQueryFilter variable please see the discussion on the first page for details on how to do this.

In the mailbox size script the database location by default is set to the root of the d: drive

strCnxn1 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d:\mbsize.mdb;"

The last hardcoded line you need to modify in the mailbox size script is the mailboxdomain name, This refers to your primary SMTP domain your using in Exchange (if your not sure what it is check the M: drive of your server or have a look at the default recipient policy)

domainname = "your.domain.com.au"

Once you have confirmed that all the scripts and databases are in the right place you are ready to test the script. With running the script the first thing you need to keep in mind is the mailbox size script must be run with an account that has full rights to all users mailboxes( this used to be referred to as service level access in Exchange 5.5). In Exchange 2000 no accounts have these rights by default and administrative accounts are specifically denied this right. Have a look at the following Microsoft Qbase article which discus's various methods that can be used to grant these rights. Once you have confirmed you have an account with the right privileges you can setup a scheduled task that executes the mailbox query script  each night (its best to keep it outside your backup window if possible).

The other thing you could do if you only want to report on a static subset of mailboxes is to forgo the first mailbox query script and just create a batch file that runs the mailbox size script for each of your users.

Next Page


Disclaimer: Your use of the information contained in these pages is at your sole risk. All information on these pages is provided "as is", without any warranty, whether express or implied, of its accuracy, completeness, fitness for a particular purpose, title or non-infringement, and none of the third-party products or information mentioned in the work are authored, recommended, supported or guaranteed by Stephen Bryant or Pro Exchange. OutlookExchange.Com, Stephen Bryant and Pro Exchange shall not be liable for any damages you may sustain by using this information, whether direct, indirect, special, incidental or consequential, even if it has been advised of the possibility of such damages.

Copyright Stephen Bryant 2008