Jul
17
2012

Exchange 2010 Mailbox report

This is not really that hard, but it’s something many Exchange admins find themselves needing:
A script which emails mailbox statistics and a schedule task which fires it offonce a month.

Now I did this for a customer with about 200 users, who just wanted some control over the mailbox sizes, as they don’t have any quota on them. For large enterprise environments, this is not necessarily the best solution, but it works.

So what I did, is compile a script, and set up a service user and a scheduled task on the Exchange 2010 server.

I’ll walk you through the steps.

First of all, here’s the script. Just copy/paste it to say “Send_mailboxsizes.ps1” or something to that effect. This needs to be locally on the server, not on a network share (simplifies privileges for the service user).

#Adding Exchange PS Snapin

Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010

#Retrieving mailboxes, sorting descending by mailbox size

$mailboxes = Get-MailboxStatistics -server * | select Displayname, @{Label=”TotalSize(MB)”;Expression={$_.TotalItemSize.Value.ToMB()}},@{Label=”DeletedItems(MB)”;Expression={$_.TotalDeletedItemSize.Value.ToMB()}},ItemCount,Lastlogontime | sort -descending “totalsize(MB)”

#Defining email header with date
$date = get-date -uformat “%d/%m/%y”
$subject = “Exchange-report ” + “$date”

#Establishing SMTP parameters
$smtpServer = “servername.domain.local”
$smtpFrom = “exchangereport@domain.com”
$smtpTo = “user@domain.com”
$messageSubject = $subject
$messageBody = $mailboxes

 #Establishing e-mail message parameters
$message = New-Object System.Net.Mail.MailMessage $smtpfrom, $smtpto
$message.Subject = $messageSubject
$message.IsBodyHTML = $true

 #Converting data to HTML
$message.Body = $mailboxes | ConvertTo-HTML

#Sending email
$smtp = New-Object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($message)

Now, you obviously need to add your details to the smtp part. But other than that it’s straight forward. The header uses EU-syntax for the date, so you might want to switch that around.
Also, switch MB to GB if you only want the GBs. I like MB better.

There are a number of ways to do this. This script generates an HTML-formatted email, which looks pretty decent. It includes names, total mailbox size, item count, recycle bin size and last logged on date. It’s powershell, so you can pretty much get anything you want, just add the variables.

Mine looks like this when I get it; without the pixelation and also with the recycle bin size.

 

Part two is setting this up on a schedule.

You’ll have to create a user, or you can use one you have allready. Simple domain user will do, but you’ll need to add it to the “Exchange View-Only Administrators”. That’s to allow it to read the statistics. This is really a bit more privileges than it needs, but it can’t really do any harm this way, so I didn’t bother creating a new role for it.

Set it to never expire on the password if you don’t have any routines for changing service user passwords.

Assign this user the “log on as a batch job”-privilege on the Exchange server using a domain GPO or the locally on the server with gpedit.msc, or on the server which you intend to run the job. It really only needs to be a server with Exchange administrative tools installed.

Create a new scheduled task, set it to “Run whether user is logged on or not” and “Run with highest privileges”, and set up the triggers you need. I set it up as a monthly occurence on the last of each month.

For the action part, it should look like this:

powershell.exe

-nologo -command “& {C:\Scripts\Send_mailboxsizes.ps1}”

 

Make sure you switch the user with your service user.

 

So that’s it. You should be all good 🙂

 

Leave a comment

*