Exchange 2010 - How to Monitor Mailbox Sizes

How to schedule a PowerShell script to automatically email a report listing user mailbox sizes.

Required Powershell Scripts

The following PowerShell script will list all mailboxes sorting by size (descending):

ExchGetMailboxSizes.ps1

Get-Mailbox -ResultSize Unlimited |
    Get-MailboxStatistics |
    Select DisplayName,StorageLimitStatus, `
        @{name="TotalItemSize (MB)"; `
            expression={[math]::Round(($_.TotalItemSize.ToString().Split("(")[1].Split(" ")[0].Replace(",","") / 1MB) ,2)} `
        }, `
       ItemCount |
    Sort "TotalItemSize (MB)" -Descending

Example Output

DisplayName  StorageLimitStatus  TotalItemSize (MB)  ItemCount
-----------  ------------------  ------------------  ---------
User 1               NoChecking            10942.95     123888
User 2               NoChecking            10307.82     122734
User 3               NoChecking            10105.58      86925
User 4               NoChecking             9746.91     162055

MailboxAlerts.ps1

This next script will send the output of the above script as an email:

$messageParameters = @{
    Subject = "[Exchange Report] Mailbox Sizes"
    Body = (.\ExchGetMailboxSizes.ps1 | Out-String)
    From = "[email protected]"
    To = "[email protected]"
    SmtpServer = "smtp.yourdomain.com"
}
Send-MailMessage @messageParameters

Now you can setup a scheduled task to run this script from your Exchange Server.

Creating a Scheduled Task

First up, copy both of the above scripts to a folder on a local disk of the Exchange Server. In the examples below I am assuming C:\Scripts.

Next, create a new Task from the Windows 2008 Task Scheduler. You should configure this task as a user who has Exchange Admin privileges.

Task Settings

General - Security Options

  • Name the task: e.g.: Weekly Mailbox Size Alerts
  • Select Run whether user is logged in or not (will require the user’s password to save the task)
  • Leave Do not store password unchecked
  • Check Run with highest privileges

Action Settings

  • Go to the Action tab and click New…

Mike Pfeiffer does a great job of explaining how to set up scheduled tasks for Exchange 2010 PowerShell Scripts. In summary, use the following settings:

Program/Script

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

Add Arguments (optional)

-version 2.0 -NonInteractive -WindowStyle Hidden -command ". 'C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1'; Connect-ExchangeServer -auto;c:\Scripts\MailboxAlerts.ps1"

Start in (optional)

C:\Scripts

Triggers

  • Setup a sensible schedule that works for you, e.g.: Weekly - Monday at 9am
  • Save the Task be clicking OK.
  • You will be prompted for your password at this stage - this is required so that the task can run when the user is logged off.

View comments.

more ...

Setting up OAuth2 for Gmail

Make sure you setup a project on the Google Developers Page using the account from which you want to send email.

See the following link describing the process of obtaining the required authorization tokens for nodemailer.

Extract of important steps

Step 1 : Installation

npm install nodemailer

Step 2 : Register your Application at Google APIs Console

XOAuth2 is similar with OAuth2, so you need to register your application to Google. Jump to Google APIs Console, then create a project if you don’t have, and open “API Access” page.

In this here, when you create a client ID, put “https://developers.google.com/oauthplayground” into the text box for Redirect URIs.

Step 3 : Open Google OAuth2.0 Playground.

You will obtain refreshToken & accessToken on this step at Google OAuth2.0 Playground. Open the page from https://developers.google.com/oauthplayground, then click the gear button on the right-top.

Set your client ID & client secret that obtained on step2, and select “Access token location:” as “Authorization header w/ Bearer prefix”. Then close it.

And set up the scopes. Put “https://mail.google.com/” into the textbox below the service scope list.

Then click [Authorize APIs] button.

Step 4 : Obtain the “refresh token”.

After OAuth2.0 authorization, click [Exchange authorization code for tokens] button. You can get your refresh token.

Use the following javascript template:

    var nodemailer = require("nodemailer");

    var smtpTransport = nodemailer.createTransport("SMTP", {
      service: "Gmail",
      auth: {
        XOAuth2: {
          user: "[email protected]", // Your gmail address.
                                                // Not @developer.gserviceaccount.com
          clientId: "your_client_id",
          clientSecret: "your_client_secret",
          refreshToken: "your_refresh_token"
        }
      }
    });

    var mailOptions = {
      from: "[email protected]",
      to: "[email protected]",
      subject: "Hello",
      generateTextFromHTML: true,
      html: "<b data-preserve-html-node="true">Hello world</b>"
    };

    smtpTransport.sendMail(mailOptions, function(error, response) {
      if (error) {
        console.log(error);
      } else {
        console.log(response);
      }
      smtpTransport.close();
    });

View comments.

more ...