none
Exchange powershell quirks RRS feed

  • Question

  • Hi,

    My goal: I want to get a list of mailboxes with their mailbox size and archive mailbox size.

    What i tried:

    1. 

    get-mailbox | select name,@{name="mailbox size";expression={Get-MailboxStatistics $_.samaccountname | select -expand TotalItemSize}},@{name="archive size";expression={Get-MailboxStatistics $_.samaccountname -archive | select -expand TotalItemSize}}

    now if i supplied a mailbox identity i only get results for the second custom column. if i leave it blank and let it run against all my mailboxes most mailboxes get both custom column empty, but towards the end the first custom column gets populated.

    so i went ahead and tried creating a custom object.

    2.

     get-mailbox | foreach {[pscustomobject]@{name = $_.name; "mailbox size" = Get-MailboxStatistics $_.samaccountname | select -expand TotalItemSize; "Archive size" = Get-MailboxStatistics $_.samaccountname -archive | select -expand TotalItemSize}}

    Now, if i ran this with a user name i get again the same result, the first column empty. but if i run this against all mailboxes i get an error 

    The pipeline was not run because a pipeline is already running. Pipelines cannot be run concurrently.
        + CategoryInfo          : OperationStopped: (Microsoft.Power...tHelperRunspace:ExecutionCmdletHelperRunspace) [], PSInvalidOperationException
        + FullyQualifiedErrorId : RemotePipelineExecutionFailed
    
    Get-MailboxStatistics : The session WinRM1, 474d9294-1fe9-4007-9dc6-aa867930c419, fcnymail1 is not available to run commands.  The session availability is Busy.
    At line:1 char:165
    + ... ive size" = Get-MailboxStatistics $_.samaccountname -archive | select ...
    +                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: ([PSSession]WinRM1:PSSession) [Invoke-Command], InvalidRunspaceStateException
        + FullyQualifiedErrorId : InvokeCommandCommandInvalidSessionAvailability,Microsoft.PowerShell.Commands.InvokeCommandCommand
    
    Get-MailboxStatistics : No valid sessions were specified.  Ensure you provide valid sessions that are in the Opened state and are available to run commands.
    At line:1 char:165
    + ... ive size" = Get-MailboxStatistics $_.samaccountname -archive | select ...
    +                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (:) [Invoke-Command], PSInvalidOperationException
        + FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.InvokeCommandCommand

    I'm beyond stumped, what am i doing wrong here?

    Any help would be greatly appreciated.

    Thanks in advance

    Monday, December 24, 2018 11:35 PM

Answers


  • Just updating, the issue seems to be only on my computer (or computers using a remote powershell session, [didn't test yet]) on the actual server my code worked beautifully.

    In any event thanks for your help.

    I didn't really check out the scripts you provided as someone on stackoverflow was able to give me a simple workaround that worked.

    this is the code that worked.

    get-mailbox <username> | Get-MailboxStatistics | select displayname,TotalItemSize,@{name="Archive size";expression={Get-MailboxStatistics $_.displayname -archive | select -ExpandProperty TotalItemSize}}

    Thanks again.

    • Marked as answer by Yeahish Monday, December 31, 2018 10:15 PM
    Monday, December 31, 2018 10:15 PM

All replies

  • Never mind, i just now found these threads
    https://social.technet.microsoft.com/Forums/windowsserver/en-US/9bf45722-b459-4045-ab27-234eb7224f3d/how-to-find-the-mailbox-size-and-archive-mailbox-size-in-the-same-powershell-output

    So it seems the issue is not an isolated case.

    I would still appreciate if anyone has a solution.

    Monday, December 24, 2018 11:42 PM
  • Hi Yeahish 

    You should be able to do this using a hash table. The below should work 

    $mailboxes = Get-Mailbox -ResultSize unlimited 
    foreach ($mailbox in $mailboxes) {
    $results = @()
    
    $props = @{
    Name = $mailbox.samaccountname
    "MailBox Size" = Get-MailboxStatistics $mailbox.samaccountname -ErrorAction SilentlyContinue |
    select -expand TotalItemSize
    "Archive size" = Get-MailboxStatistics $mailbox.samaccountname -archive -ErrorAction SilentlyContinue | 
    select -expand TotalItemSize
    
    }
    $Results += New-Object psobject -Property $props
    $Results | Select Name,"MailBox Size","Archive size"
    }


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.

    Tuesday, December 25, 2018 12:33 AM
  • Hi Yeahish,

    This script will be useful to you:

    $Mailboxes = Get-Mailbox -ResultSize unlimited | where {$_.RecipientTypeDetails -eq "UserMailbox"}
    $Data = @()
    
    Foreach ($Mailbox in $Mailboxes)
    {
        $temp = Get-Mailbox $Mailbox | Select name,@{Expression={};Label = "MailboxSize"}, @{Expression={};Label = "ArchiveSize"}
        $temp.MailboxSize = (Get-MailboxStatistics $Mailbox.samaccountname).TotalItemSize
    
        if((Get-Mailbox $Mailbox).ArchiveDatabase){
            $temp.ArchiveSize = (Get-MailboxStatistics $Mailbox.samaccountname -archive).TotalItemSize
        }else{
            $temp.ArchiveSize = "Not Enabled"
        }   
        $Data += $temp   
    }
    $Data | Export-Csv c:/temp/123.csv -NoTypeInformation

    The result for this script:

    Regards,

    Kyle Xu


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnsf@microsoft.com.

    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    Tuesday, December 25, 2018 7:16 AM
    Moderator

  • Just updating, the issue seems to be only on my computer (or computers using a remote powershell session, [didn't test yet]) on the actual server my code worked beautifully.

    In any event thanks for your help.

    I didn't really check out the scripts you provided as someone on stackoverflow was able to give me a simple workaround that worked.

    this is the code that worked.

    get-mailbox <username> | Get-MailboxStatistics | select displayname,TotalItemSize,@{name="Archive size";expression={Get-MailboxStatistics $_.displayname -archive | select -ExpandProperty TotalItemSize}}

    Thanks again.

    • Marked as answer by Yeahish Monday, December 31, 2018 10:15 PM
    Monday, December 31, 2018 10:15 PM