What is this?

This knowledgebase contains questions and answers about PRTG Network Monitor and network monitoring in general.

Learn more

PRTG Network Monitor

Intuitive to Use. Easy to manage.
More than 500,000 users rely on Paessler PRTG every day. Find out how you can reduce cost, increase QoS and ease planning, as well.

Free Download

Top Tags

View all Tags

Exchange Top 20 Mailbox Sensor



Hi there, I'm trying to write a custom sensor to return the Top 20 Mailboxes in Exchange 2010 ordered by Size however I'm not having much luck.

Here is the script:

#start new remote session from powershell

$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://fmex1/PowerShell/ -Authentication Kerberos

#import new session

Import-PSSession $Session 
#Add-PsSnapin "Microsoft.Exchange.Management.PowerShell.E2010" -ErrorAction SilentlyContinue -WarningAction SilentlyContinue 
$free=@{Label="Size(GB)"; Expression={$_.TotalItemSize.Value.ToGb()}} -First 20 | ft -auto
$server=Get-Mailbox -Server $servername Get-MailboxDatabase -Status | select DisplayName,$free
#PRTG Formatting 

Write-Host "<prtg>"

foreach ($_.DisplayName in $server)  {


Write-Host "</prtg>"

Exit 0

The text file that get written to the logs folder outputs:
sensors\EXEXML\ExTop20.ps1:15 char:12
+ foreach ($_.DisplayName in $server)  {
+            ~
Missing 'in' after variable in foreach loop.
At C:\Program Files (x86)\PRTG Network Monitor\custom 
sensors\EXEXML\ExTop20.ps1:15 char:35
+ foreach ($_.DisplayName in $server)  {
+                                   ~
Unexpected token ')' in expression or statement.
    + CategoryInfo          : ParserError: (:) [], ParseException
    + FullyQualifiedErrorId : MissingInInForeach

I don't understand because there is definitely an "in" in the foreach loop..

Any assistance would be fantaastic!

custom-sensor exchange-2010 exchange-powershell powershell

Created on Feb 17, 2015 3:09:29 AM

Last change on Feb 17, 2015 1:07:41 PM by  Torsten Lindner [Paessler Support]

3 Replies



I made some changes, completely rewrote the script. The script runs fine in Exchange Shell and brings back the top 20 mailboxes ordered by largest to smallest.

When PRTG runs the script, it seems to pick 20 random mailboxes. So it seems to ignore the "sort-object TotalItemSize -Descending" part of the command.

I've tried moving it around and even having the sort as a secondary function but still no luck. I keep getting the same unordered mailboxes back.

Is someone able to help?

Here is the script:


$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri $CURI -Authentication Kerberos
Import-PSSession $Session -DisableNameChecking

$mbs = Get-Mailbox | Get-MailboxStatistics | Sort-object TotalItemSize -Descending | Select DisplayName,TotalItemSize | select -first 20

$result= "<?xml version=`"1.0`" encoding=`"Windows-1252`" ?>`r`n"

foreach($entry in $mbs)
	$result+="   <result>`r`n"
	$result+="       <channel>Mailbox: "+$entry.DisplayName+"</channel>`r`n"
	$result+="       <unit>Size (GB)</unit>`r`n"
	$result+="       <value>"+([int](($entry.TotalItemSize.split("(")[0]).replace(" ","")/1GB))+"</value>`r`n"
	$result+="   </result>`r`n"
$result+="   <text>OK</text>`r`n"
remove-pssession -session $Session
Exit 0

As I said, the script runs fine. It's just not sorting the results properly.


Created on Feb 17, 2015 12:43:44 PM

Last change on Feb 17, 2015 1:07:55 PM by  Torsten Lindner [Paessler Support]



While the script looks very good, only the unit should be:

$result+="      <customunit>Size (GB)</customunit>`r`n"

I'm afraid, the type of result it achieves, is not really compatible for PRTG. Essentially you get a dynamic list back here (sorted), with each mailbox being one sensor channel. As it is not possible to remove sensor channels, this sensor would have a growing list of sensors, and it cannot be sorted according to mailbox-size in PRTG's interface.
While the check is indeed very interesting, getting such a Toplist into PRTG is not possible. Consider saving the toplist into file(s) outside of PRTG, and only returning numerical values to PRTG (maybe total mailbox size or similar).

Created on Feb 18, 2015 7:51:54 AM by  Torsten Lindner [Paessler Support]

Last change on Feb 18, 2015 7:52:34 AM by  Torsten Lindner [Paessler Support]



Ah, okay! That would make sense as to why it's returning the exact same users each time.

I'm wondering if I could instead do something with multiple values?

We are currently running this script daily and having the result emailed however we wanted to keep a closer eye on their mailbox sizes so we were to know if they are cleaning it up like we ask.

It's a bit of a bummer that I can't monitor this top 20 list in real time. Thanks for the reply anyway!

Created on Feb 18, 2015 9:54:02 PM

Disclaimer: The information in the Paessler Knowledge Base comes without warranty of any kind. Use at your own risk. Before applying any instructions please exercise proper system administrator housekeeping. You must make sure that a proper backup of all your data is available.