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 
$servername=fmex1
$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)  {
$Name=$_.DisplayName
$Data=$_.TotalItemSize
Write-Host
  "<result>"
    "<channel>$Name</channel>"
    "<value>$Data</value>"
  "</result>"

}


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!


Article Comments

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:

$CURI="http://fmex1/PowerShell/"

$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"
$result+="<prtg>`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"
$result+="</prtg>`r`n"
$result
remove-pssession -session $Session
Exit 0

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

Thanks!


Feb, 2015 - Permalink

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).


Feb, 2015 - Permalink

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!


Feb, 2015 - Permalink