New Question
PRTG Network Monitor

Intuitive to Use.
Easy to manage.

200.000 administrators have chosen PRTG to monitor their network. Find out how you can reduce cost, increase QoS and ease planning, as well.

Download >>


What is this?

This knowledgebase contains questions and answers about PRTG Network Monitor and network monitoring in general. You are invited to get involved by asking and answering questions!

Learn more


Top Tags

View all Tags

Exchange Top 20 Mailbox Sensor



Your Vote:



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 
[email protected]{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 by  wjbeckett (0) 1

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

3 Replies



Your Vote:



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 by  wjbeckett (0) 1

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



Your Vote:



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]



Your Vote:



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 by  wjbeckett (0) 1

Please log in or register to enter your reply.

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.