Friday, March 11, 2005

Relating Mailbox sizes to Mailbox quotas

Somebody asked last week about how to query mailbox sizes and mailbox quota information for a user. This is a interesting question there’s are a lot of different methods for querying mailboxes sizes most of these are listed in this KB . With Exchange 2003 you can use WMI to pull the mailbox size from the Exchange_mailbox class there’s a sample in the Exchange SDK here. With mailbox quotas what quota information will apply to your mailbox will differ depending on how quotas have been applied to the mailbox. Quotas can come from 1 of 3 locations, from a policy applied to the mail-store, by setting the quotas on the mail store itself and from quotas set on the mailbox directly. So to work out what quotas apply to a user you first need to query the user object in AD and look at the mDBUseDefaults property (which relates to the use store defaults check box in AD Users and Computers). If this is set to true (which is default) then you need to look at the mail-store object to see if quotas have been applied to the store. After you have the mailbox quota information you can query the server via WMI to work out the mailbox size and then using a little math you can determine the percentage of the quota that’s been used for each user.

I came up with a couple of scripts that could do this based on the email address of the user. These scripts are designed to run with the users email address as a commandline parameter it does a lookup of Active directory based on the email address. Then checks the mDBUseDefaults property. If this is set to true it opens the mail-store object this mailbox is located on using the homemdb property and reads the quota information of the mail-store. If the mDBUseDefaults is set to false the quota information is read from the user object. The script then uses WMI to workout the mailbox size this is where the two scripts differ. The first one use’s a select statement with a Where clause and the second use the 5 key fields for the exchange_mailbox WMI class to do a direct connection via WMI. I thought the second method may have been quicker but it still a little slower then I’d like. The information is then echo'd out to the command-line and a little math I used to work out the percentage of the Quota used. These scripts are okay for one user but if your trying to do multiple users (like all the users on a server) this method is pretty slow. I started looking into ways to make this faster and one method that seemed to work although it has taken me most of the week to get it going is to use ADO data shaping to combine the data from the multiple datasources into one ADO recordset. I’m still working on this example but I should have something that’s post worthy by the next week. I’ve posted both the script I’ve talked about here as well as the slow server example.