Exchange powershell .Value.toMB()

If you run exchange powershell, you have the ability to use .Value.toMB() ie. on the ProhibitSendQuota value, or on the IssueWarningQuota, to recalculate this value to MB’s instead of bytes (and it omits the GB addition). But if you create a (remote) session to Exchange powershell, this functionality stops working. To create a (remote) session to Exchange powershell, you can run commands like this:

$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://<Exchange server>/PowerShell/ -Authentication Kerberos
Import-PSSession $Session -DisableNameChecking | Out-Null

To disconnect this session, you can use:

Remove-PSSession $Session | Out-Null

I noticed this behavior, when I was creating a script to monitor the quota’s for mailboxes. Within Exchange powershell this worked great:

$Results = @()
$Mailboxes = Get-Mailbox -ResultSize Unlimited | Where {$_.UseDatabaseQuotaDefaults -eq $false } | Select SamAccountName,DisplayName,@{label="ProhibitSendQuota";expression={$_.ProhibitSendQuota.Value.toMB()}},@{label="IssueWarningQuota";expression={$_.IssueWarningQuota.Value.toMB()}}
Foreach($Mailbox in $Mailboxes)
$MailboxSize = $null
$MailboxSize = Get-MailboxStatistics -Identity $Mailbox.SamAccountName | Select @{label="TotalItemSize";expression={$_.TotalItemSize.Value.toMB()}}
$Result = New-Object -TypeName PSObject
$Result | Add-Member -Name 'Mailbox' -MemberType NoteProperty -Value $Mailbox.DisplayName
$Result | Add-Member -Name 'ProhibitSendQuota' -MemberType NoteProperty -Value $Mailbox.ProhibitSendQuota
$Result | Add-Member -Name 'IssueWarningQuota' -MemberType NoteProperty -Value $Mailbox.IssueWarningQuota
$Result | Add-Member -Name 'TotalItemSize' -MemberType NoteProperty -Value $MailboxSize.TotalItemSize
$PercentageUsed = "{0:N2}" -f ($MailboxSize.TotalItemSize / $Mailbox.ProhibitSendQuota * 100)
$Result | Add-Member -Name 'PercentageUsed' -MemberType NoteProperty -Value $PercentageUsed.Replace(".",",")
$Results += $result

Since my exchange server is using US culture settings, but I need to see the results on a computer with EU (Dutch) culture settings, I need to do the conversion in the end $PercentageUsed.Replace(".",","), to replace the dot with a comma, otherwise it’ll give me wrong results (Why? see my post on [Decimal] conversion has some unexpected behavior depending on culture settings)

But if I run this remotely, or from the ‘normal’ powershell shell on the exchange server (and connecting to Exchange powershell), this script will fail, because the function .Value.toMB() doesn’t exist. I had to rewrite the script and edit the results, to convert it to MB’s myself. Since exchange returns both bytes and a GB result, this means I have to get a substring of the results. I achieved this by changing my script to this (too bad it is a lot more code):

$Results = @()
$Mailboxes = Get-Mailbox -ResultSize Unlimited | Where {$_.UseDatabaseQuotaDefaults -eq $false } | Select SamAccountName,DisplayName,@{label="ProhibitSendQuota";expression={$_.ProhibitSendQuota.Substring($_.ProhibitSendQuota.IndexOf("(") + 1).Replace(" bytes)","").Replace(",","")}},@{label="IssueWarningQuota";expression={$_.IssueWarningQuota.Substring($_.IssueWarningQuota.IndexOf("(") + 1).Replace(" bytes)","").Replace(",","")}}
Foreach($Mailbox in $Mailboxes)
$MailboxSize = $null
$TempSize = $null
$ProhibitSendQuota - $null
$IssueWarningQuota = $null
$TotalSizeMB = $null
$MailboxSize = Get-MailboxStatistics -Identity $Mailbox.SamAccountName | Select TotalItemSize
[string]$TempSize = $MailboxSize.TotalItemSize.Value
[long]$TotalSize = $TempSize.Substring($TempSize.IndexOf("(") + 1).Replace(" bytes)","").Replace(",","")
$Result = New-Object -TypeName PSObject
$Result | Add-Member -Name 'Mailbox' -MemberType NoteProperty -Value $Mailbox.DisplayName
If($Mailbox.ProhibitSendQuota -ne "Unlimited") { $ProhibitSendQuota = "{0:N0}" -f ($Mailbox.ProhibitSendQuota / 1MB) }
Else { $ProhibitSendQuota = $Mailbox.ProhibitSendQuota }
If($Mailbox.IssueWarningQuota -ne "Unlimited") { $IssueWarningQuota = "{0:N0}" -f ($Mailbox.IssueWarningQuota / 1MB) }
Else { $IssueWarningQuota = $Mailbox.IssueWarningQuota }
$Result | Add-Member -Name 'ProhibitSendQuotaMB' -MemberType NoteProperty -Value $ProhibitSendQuota
$Result | Add-Member -Name 'IssueWarningQuotaMB' -MemberType NoteProperty -Value $IssueWarningQuota
$TotalSizeMB = "{0:N0}" -f ($Totalsize / 1MB)
$Result | Add-Member -Name 'TotalItemSizeMB' -MemberType NoteProperty -Value $TotalSizeMB
If($Mailbox.ProhibitSendQuota -eq "Unlimited") { $PercentageUsed = 0 }
Else { $PercentageUsed = "{0:N2}" -f ($TotalSize / $Mailbox.ProhibitSendQuota * 100) }
$Result | Add-Member -Name 'PercentageUsed' -MemberType NoteProperty -Value $PercentageUsed
$Results += $result

As you can see, it is a lot more code, but it does the (same) job. At least there isn’t an issue with decimal conversion anymore, though sorting based upon size doesn’t work (I thought this was because of the difference in culture settings on both of the servers, but I also have this issue if I run this on the exchange server in the ‘normal’ powershell environment). It was because the value was now a string and not an int. So I had to sort it as int, thus this was solved by using Sort-Object {$_.PercentageUsed -as [int]} -Descending, like in this command:

$Results | Select Mailbox, ProhibitSendQuotaMB, IssueWarningQuotaMB, TotalItemSizeMB, PercentageUsed | Sort-Object {$_.PercentageUsed -as [int]} -Descending

You can download the entire script here

One thought on “Exchange powershell .Value.toMB()

  1. Ran into the exact same issue here with the exact same circumstances (culture dutch). The IssueWarningQuota value is returned as a string instead of an integer so I couldn’t convert it. Formatting the results as you did in your script fixed the issue for me.

    Many thanks!


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s