Share via


Powershell results in one single line

Question

Tuesday, April 7, 2015 11:57 AM

Hi all,

To achieve a powershell script to monitor Exchange mailbox quotas via Nagios.
I want to understand how to get the results of a PowerShell command on one single line.

Here is the command line used to list mailboxs whose quota is reached :

Get-MailboxDatabase | Get-MailboxStatistics | Where-Object {$_.StorageLimitStatus -match 'IssueWarning|ProhibitSend|MailboxDisabled'} | Select-Object DisplayName,StorageLimitStatus

The result :

DisplayName                                                  StorageLimitStatus
                                                  
User10                                                         IssueWarning
User11                                                         IssueWarning

To call the script via Nagios, I would need that the output of the command above or in one line.
Ex :

WARNING : User10 - IssueWarning ; User11 - IssueWarning

Possible ? Do you have any clues to help me with this script?

All replies (4)

Tuesday, April 7, 2015 12:09 PM âś…Answered | 1 vote

Hi Sixela,

this should do the trick:

$res = Get-MailboxDatabase | Get-MailboxStatistics | Where-Object { $_.StorageLimitStatus -match 'IssueWarning|ProhibitSend|MailboxDisabled' } | Select-Object DisplayName, StorageLimitStatus
if ($res) { "WARNING : " + ( ($res | %{ "$($_.DisplayName) - $($_.StorageLimitStatus)" }) -join " ; ") }

In Step one you retrieve the Mailboxes to report and store them in a variable.
In Step two, only if you found entries, you write the warning.

  • In the inner braces you convert each result set into a single string.
  • In the outer braces you then combine these strings with the semicolon as delimiter

Cheers,
Fred

There's no place like 127.0.0.1


Tuesday, April 7, 2015 12:30 PM

Thank you for your quick response.

But the result contains "WARNING:" no more information

[PS] C:\Users\user\scripts>.\Quota.ps1
WARNING :

 

   


Tuesday, April 7, 2015 12:34 PM

Hi Sixela,

my bad, I found the error in it and fixed it (edited the code in the previous post). Try that again, this time it should work properly.

Cheers,
Fred

There's no place like 127.0.0.1


Tuesday, April 7, 2015 12:40 PM

It works.
Thank you very much !