get-mailfolderstatistics size formatting question RRS feed

  • Question

  • Hello,

    I'm populating table with mail folders statistic. But for some reasons I cannot to convert folders size into numeric. Script just gives errors. However script works fine for some folders, but gives errors when I'm trying to use ToKB() for some folders (f.e. Calendar). It shows right size without conversion.

    Here is piece of code.

    Get-MailboxFolderStatistics $DistName -domaincontroller $DomContr| ForEach-Object{

    $ficount = 0
    $fisize = 0
    $fsisize = 0
    $fscount = 0
    $FolderName = $_.Name
    "Folder name: " + $FolderName
    if ($_.FolderSize -ne $null -or $_.FolderSize -ne 0) {$fisize = $_.FolderSize }
    "Folder size: " + $fisize
    if ($_.ItemsInFolder -ne $null){$ficount = $_.ItemsInFolder}
    "Items in folder:" + $ficount
    if ($_.ItemsInFolderAndSubfolders -ne $null){$fscount = $_.ItemsInFolderAndSubfolders}

    if ($_.FolderAndSubfolderSize -ne $null) {$fsisize = $_.FolderAndSubfolderSize.tokb()}

    "Subfolder size: " +$fsisize
    "Folder path: " + $_.FolderPath


    How can I fix this problem?

    Thank you.

    Tuesday, December 13, 2011 3:12 PM

All replies

  • What error is returned?

    Are you running this from a native Exchange Management Shell, or with a remote session in Exchange 2010?

    The script works fine for me, in a native Exchange Management Shell (2010), but fails as "[System.String] doesn't contain a method named 'toKB'" when run from a remote session, as the returned objects are not of type "Microsoft.Exchange.Data.ByteQuantifiedSize", but are rather "System.String"


    My Blog: http://unlockpowershell.wordpress.com
    My Book: Windows PowerShell 2.0 Bible
    My E-mail: -join ("6B61726C6D69747363686B65406D742E6E6574"-split"(?<=\G.{2})",19|%{[char][int]"0x$_"})
    Tuesday, December 13, 2011 6:42 PM
  • I'm running it in the Exchange 2010 management shell. Here is screen output for the last two folders. As you can see it works for some folders, but don't work for others.


    Folder name: Blackberry
    Folder size: 102 KB (104,487 bytes)
    ITems in folder:6
    Subfolder size: 102
    Folder path: /Blackberry

    Folder name: Calendar
    Folder size: 14.21 MB (14,898,383 bytes)
    ITems in folder:583
    ForEach-Object : Method invocation failed because [System.String] doesn't contain a method named 'tokb'.
    At C:\program files\exchutil\scripts\test.ps1:80 char:82
    + Get-MailboxFolderStatistics $DistName -domaincontroller $DomContr| ForEach-Object <<<< {
        + CategoryInfo          : InvalidOperation: (tokb:String) [ForEach-Object], RuntimeException
        + FullyQualifiedErrorId : MethodNotFound,Microsoft.PowerShell.Commands.ForEachObjectCommand

    Tuesday, December 13, 2011 7:35 PM
  • I'd lay odds that you are NOT using the Exchange Management Shell, as the error clearly shows that the object is system.string.

    Run the following command:


    If it returns "The term 'get-excommand' is not recognized as the name of a cmdlet, function, script file, or operable program", you are not running the Exchange Management Shell.

    How are you loading the shell?

    Click Start...All Programs...Microsoft Exchange Server 2010...Exchange Management Shell and retry?

    Also, the script does not work for som folders - the If statements make it appear to work.


    My Blog: http://unlockpowershell.wordpress.com
    My Book: Windows PowerShell 2.0 Bible
    My E-mail: -join ("6B61726C6D69747363686B65406D742E6E6574"-split"(?<=\G.{2})",19|%{[char][int]"0x$_"})
    Tuesday, December 13, 2011 8:01 PM
  • get-excommand returned me bunch of different commands.  I'm running Exchange Shell from Start...All Programs...Microsoft Exchange Server 2010...Exchange Management Shell

    This script worked fine in the test lab, but doesn't work on priduction. Please note, script returns size of folders in string format, but fails when it I'm trying to convert it to numeric.

    Tuesday, December 13, 2011 9:02 PM
  • That's very strange.

    Can you try it on an Exchange server?

    Also, do this:

    $Stats = Get-MailboxFolderStatistics <your account>
    $stats[0].FolderAndSubfolderSize  |gm

    What TypeName is returned?


    My Blog: http://unlockpowershell.wordpress.com
    My Book: Windows PowerShell 2.0 Bible
    My E-mail: -join ("6B61726C6D69747363686B65406D742E6E6574"-split"(?<=\G.{2})",19|%{[char][int]"0x$_"})
    Tuesday, December 13, 2011 9:15 PM
  • Actually it is Exchange server. We have 30+ Exchange 2010 servers, so I'm trying to figure out how it works.

       TypeName: Microsoft.Exchange.Data.ByteQuantifiedSize

    Name          MemberType Definition
    ----          ---------- ----------
    CompareTo     Method     int CompareTo(Microsoft.Exchange.Data.ByteQuantifiedSize other)
    Equals        Method     bool Equals(System.Object obj), bool Equals(Microsoft.Exchange.Data.ByteQuantifiedSize other)
    GetHashCode   Method     int GetHashCode()
    GetType       Method     type GetType()
    RoundUpToUnit Method     System.UInt64 RoundUpToUnit(Microsoft.Exchange.Data.ByteQuantifiedSize+Quantifier quantifier)
    ToBytes       Method     System.UInt64 ToBytes()
    ToGB          Method     System.UInt64 ToGB()
    ToKB          Method     System.UInt64 ToKB()
    ToMB          Method     System.UInt64 ToMB()
    ToString      Method     string ToString(), string ToString(string format), string ToString(string format, System.IF...
    ToTB          Method     System.UInt64 ToTB()

    Tuesday, December 13, 2011 11:40 PM
  • Karl,


    I'm trying to run the Get-MailboxFolderStatistics from a remote session and it won't export the data for me. Can you explain your comment about:

    "Microsoft.Exchange.Data.ByteQuantifiedSize", but are rather "System.String" ...

    a little more.


    Is there a way to get this information exported from a Powershell environment as opposed to an Exchange Management Shell environment? I have a script that works great in EMS but when running from Powershell it fails to return the FolderSize or FolderandSubfolderSize information.

    This is for support staff...

    Script below.. some items would need to be modified for specific environments as noted:

    ### Modify the URL below to an Server with the Exchange 2010 Installed
    $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://EXCHANGE_SERVER.DOMAIN.COM/PowerShell/ -Authentication Kerberos
    Import-PSSession $Session -ErrorAction SilentlyContinue

    ### This quoted line below will ask the user to enter in the mailbox that needs to be looked at
    $UserMailbox = read-host "Enter Email Address of mailbox Statistics to be Found"

    ### The following is for the email HTML formatting - it can be modified as needed as far as color and appearance
    $a = "<style>"
    $a = $a + "BODY{background-color:maroon;}"
    $a = $a + "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}"
    $a = $a + "TH{border-width: 1px;padding: 3px;border-style: solid;border-color: black;background-color:gray}"
    $a = $a + "TD{border-width: 1px;padding: 3px;border-style: solid;border-color: black;background-color:white}"
    $a = $a + "</style>"

    $body = get-mailboxfolderstatistics -identity $UserMailbox | select Name,ItemsInFolder,@{label="Folder & Subfolder Size MB";expression = {$_.folderandsubfoldersize.ToMB()}} | ConvertTo-HTML -head $a -body "<H2><font color=white>$UserMailbox Mailbox Size</font color></H2>"

    Send-MailMessage -smtpServer "exchange_server.domain.com" -To User@domain.com -From User@domain.com -Subject "$UserMailbox Mailbox Size Report" -Body ($body | Out-String) -BodyAsHtml

    Remove-PSSession $Session

    Monday, January 16, 2012 5:01 PM