none
Powershell script to run Get-MailboxFolderStats against 5k+ users without time out. RRS feed

  • Question

  • We are running the below script to get the stats of calendar folder for 5k+ users but the powershell times out after sometime. We need to find a way to run this script completely.

    $mbx = invoke-command -session (get-pssession) -scriptblock {get-mailbox -Filter '(RecipientTypeDetails -eq "UserMailBox")' -resultsize unlimited | Select-Object -ExpandProperty userprincipalname} 
    foreach ($name in $mbx) {Get-MailboxFolderStatistics -Identity $name -FolderScope calendar | ?{$_.itemsinfolder -ge 10000} | select-object userprincipalname, Identity, ItemsInFolder, FolderSize | Export-csv e:\MBX_Cal_Stats_060918.csv -NoTypeInformation -Append}

    Please suggest. Thanks!!

    • Moved by Bill_Stewart Monday, July 30, 2018 1:47 PM This is not "scripts on demand"
    Tuesday, June 12, 2018 5:45 PM

All replies

  • $sb = {
        Get-Mailbox -Filter 'RecipientTypeDetails -eq "UserMailBox' -resultsize unlimited |
            Get-MailboxFolderStatistics -FolderScope calendar |
            Where-Object{$_.itemsinfolder -ge 10000}
    }
    $sess = New-PSSession ....
    $mbxstats = invoke-command -session $sess -scriptblock $sb | 
        select-object userprincipalname, Identity, ItemsInFolder, FolderSize | 
        Export-csv e:\MBX_Cal_Stats_060918.csv -NoTypeInformation


    \_(ツ)_/

    Tuesday, June 12, 2018 6:03 PM
  • Thanks for the reply. I am getting the attached error.

    PS C:\Users\> $sb = {

        Get-Mailbox -Filter 'RecipientTypeDetails -eq "UserMailBox' -resultsize unlimited |
            Get-MailboxFolderStatistics -FolderScope calendar |
            Where-Object{$_.itemsinfolder -ge 10000}
    }
    $sess = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
    $mbxstats = invoke-command -session $sess -scriptblock $sb | 
        select-object userprincipalname, Identity, ItemsInFolder, FolderSize | 
        Export-csv e:\MBX_Cal_Stats_061218.csv -NoTypeInformation

    The syntax is not supported by this runspace. This can occur if the runspace is in no-language mode.
        + CategoryInfo          : ParserError: (
        Get-Mailb...er -ge 10000}
    :String) [], ParseException
        + FullyQualifiedErrorId : ScriptsNotAllowed
        + PSComputerName        : outlook.office365.com

    Tuesday, June 12, 2018 7:18 PM
  • This is how we do this in O365.  Invoke-Cpmmand cannot be used remotely.

    $sess = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
    Import-PSSession $sess
    
    $mbxstats = Get-Mailbox -Filter 'RecipientTypeDetails -eq "UserMailBox"' -resultsize unlimited |
            Get-MailboxFolderStatistics -FolderScope calendar
    
    $mbxstats | Where-Object{$_.itemsinfolder -ge 10000} |
        select-object userprincipalname, Identity, ItemsInFolder, FolderSize | 
        Export-csv e:\MBX_Cal_Stats_060918.csv -NoTypeInformation


    \_(ツ)_/

    Tuesday, June 12, 2018 7:39 PM