Question

I'm trying to create simple reports on Exchange 2010 mailbox size.

While this works:

Get-MailboxStatistics -server <serverfqdn> |ft  displayname, TotalItemSize

this doesn't (second column stays empty):

Get-MailboxStatistics -server <serverfqdn> |ft  displayname, {$_.TotalItemSize.Value.ToBytes()}

The problem is that I need the size as an integer, so the first line of code doesn't serve my purpose. According to several websites the second line of code should work but unfortunately doesn't on my machine.

I know I could parse the value after using the first line but that would be unnecessarily inefficient, wouldn't it? :-)

Hope anyone can help.

Regards, Kevin

Was it helpful?

Solution

If performing from a imported PS session the methods .ToMB() is lost as the type becomes a custom object. The $variable.TotalItemSize.Value becomes a two element array [0] in KB,MB or GB and [1] always in bytes.

So to use this we can play with strings to achieve what we want.. in long hand for clarity

$mailboxes = Get-Mailbox -Filter{(RecipientType -eq "UserMailbox") -and (CustomAttribute12 -eq "whatever")}
foreach ($mailbox in $mailboxes)
{
$size1 = Get-MailboxStatistics $mailbox.id 
[string]$bob = $size1.TotalItemSize.Value
[int]$bill = $bob.TrimEnd(" bytes)").Split("(")[1] # The bytes part of the array.
$bill = $bill/1mb # Convert into MB's
if ($bill -le 1500) {do something} Else {"Too Big " + $bill} # note -le 1500 NOT 1500MB

}

I hope this helps

OTHER TIPS

this worked for me

$a = get-mailbox -id user | Get-MailboxStatistics 

$a.TotalItemSize.Value.ToMB()

$a.TotalItemSize.Value.ToKB()

I have the same issue. I'm not sure if you resolved this. I have this, which is quite ugly - but works:

$a = get-mailbox USER | get-mailboxstatistics
$intTotalItemSize = [int]$a.TotalItemSize.SubString($a.TotalItemSize.indexof("(")+1, $a.TotalItemSize.indexof(" b")-$a.TotalItemSize.indexof("("))

Try this for your size expression:

@{expression={$_.TotalItemSize.Value.ToMB()};label="Mailbox Size(MB)"}

I believe there is also a ToKB() method.

MVP Shay Levy has delved into this on his blog (http://blogs.microsoft.co.il/blogs/scriptfanatic/archive/2011/08/22/get-full-control-over-your-exchange-remote-powershell-session.aspx).

Basically, you have to modify a setting in the PowerShell virtual directory on the server that you are remoting to.

This is great news for those who are remoting to Exchange servers that they have this kind of control over, but is not helpful for those of us who use hosted Exchange solutions and cannot change these settings. I suppose we will just have to abandon some of the uber-coolness of PowerShell and go back to parsing the string to get the bytes and convert from there.

--EDIT--

This is how I tackled outputting a file of all of my users' mailbox sizes. It could be compressed a bit further, but is a little more readable this way.

$allMailboxes = Get-Mailbox -ResultSize Unlimited

ForEach ( $mailbox in $allMailboxes ) {
    $itemSizeString = ( Get-MailboxStatistics $mailbox.Identity ).TotalItemSize.Value.ToString()
    $posOpenParen = $itemSizeString.IndexOf("(") + 1
    $numCharsInSize = $itemSizeString.IndexOf(" bytes") - $posOpenParen 
    $mailboxSizeInBytes = $itemSizeString.SubString($posOpenParen,$numCharsInSize).Replace(",","")

    Write-Output "$($mailbox.alias),$($mailboxSizeInBytes)"
}

Please, see this article: http://blogs.technet.com/b/gary/archive/2010/02/20/the-get-mailboxstatistics-cmdlet-the-totalitemsize-property-and-that-pesky-little-b.aspx

Get-Mailbox | Get-MailboxStatistics | Add-Member -MemberType ScriptProperty -Name TotalItemSizeinMB -Value {$this.totalitemsize.value.ToMB()} -PassThru | Format-Table DisplayName,TotalItem*

I needed to have this work outside of a remoting session, so I simplified the answer from Greybear to this:

$a = get-mailbox USER | get-mailboxstatistics
$intTotalItemSize = [int64]($a.TotalItemSize -split '[\( ]')[3]

Or in the format of the original question::

Get-MailboxStatistics -Server <serverfqdn> | Select-Object -Property DisplayName,@{label="TotalItemSize";expression={[int64]($_.TotalItemSize -split '[\( ]')[3]}} | ft

Realized that [int] would fail for mailboxes over 4GB, so changed to [int64]. Alternately, display the mailboxes in MB:

Get-MailboxStatistics -Server <serverfqdn> | Select-Object -Property DisplayName,@{label="TotalItemSize";expression={[int64](([int64]($_.TotalItemSize -split '[\( ]')[3])/1048576)}} | ft

The name needs to go before the expression. This will work.

Get-MailboxStatistics -Identity [name] | select @{label=”User”;expression={$_.DisplayName}},lastlogontime,@{label=”Total Size (MB)”;expression={$_.TotalItemSize.Value.ToMB()}}

This works for me

@{Name="TotalSize (MB)"; Expression={((($_.TotalItemSize) -split " ")[0])}}

You might try:

get-mailbox -resultsize unlimited | Get-MailboxStatistics | ft displayname,@{label="Total Size (MB)";expression={$_.TotalItemSize.Value.ToMB()}}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top