Graham J
Graham J

Reputation: 497

PowerShell Exchange, easier way to work out available space left before hitting quota?

Afternoon all,

I'm trying to work out the available space left in MB on a users mailbox before they hit the ProhibitSendQuota.

I have the following which works, but seems an insanely long winded way to achieve it.

Note : I am using a remote PowerShell Session. I cannot use Exchange Management Tools to achieve this otherwise I'd use .ToMB()

$7X2_MBX      = Get-Mailbox $7X2_USER_AD.SamAccountName
$7X2_MBXSTATS = Get-MailboxStatistics $7X2_USER_AD.SamAccountName

$a = ( ( ( ( $7X2_MBX.ProhibitSendQuota | Out-String ).Split( "(" )[1] ) -replace "," , "" ).Split( ")" )[0] -replace " bytes" , "" ) - ( ( ( ( $7X2_MBXSTATS.TotalItemSize.Value | Out-String ).Split( "(" )[1] ) -replace "," , "" ).Split( ")" )[0] -replace " bytes" , "" )

[math]::round( ( $a / 1048576 ) ,2 )

Like I say, this seems to work. If it's a pretty decent way of getting there then I'm impressed I managed it myself! But definitely seems long winded.

Upvotes: 1

Views: 67

Answers (2)

Avshalom
Avshalom

Reputation: 8889

Here's mine:

$7X2_MBX      = Get-Mailbox $7X2_USER_AD.SamAccountName
$7X2_MBXSTATS = Get-MailboxStatistics $7X2_USER_AD.SamAccountNam

$Quota = ([int64]($7X2_MBX.ProhibitSendQuota -split '[\( ]')[3]) 
$MailboxSize = [int64]($7X2_MBXSTATS.TotalItemSize -split '[\( ]')[3]

"Quota Available {0:N2} GB" -f (($Quota - $MailboxSize) / 1GB)

Upvotes: 2

sodawillow
sodawillow

Reputation: 13176

Here is how I would do it:

function Remove-UselessText ($inputString) {
    $output = ($inputString -replace "[0-9\.]+ [A-Z]* \(([0-9,]+) bytes\)","`$1") -replace ",",""
    [int]$output
}

$accountName  = $7X2_USER_AD.SamAccountName
$7X2_MBX      = Get-Mailbox $accountName
$7X2_MBXSTATS = Get-MailboxStatistics $accountName

$freeQuota = (Remove-UselessText $7X2_MBX.ProhibitSendQuota) - (Remove-UselessText $7X2_MBXSTATS.TotalItemSize.Value)

"Free quota for $accountName : {0:N2} MB" -f ($freeQuota / 1MB)

Upvotes: 2

Related Questions