How to properly use -resultsize unlimited switch when perfoming bulk object query in office365

Benard Mwanza 996 Reputation points
2022-04-13T08:08:45.887+00:00

Have created a PowerShell script to export mailbox statistics in office365. The script accepts a csv file that has the list of users who I want to export that information.

The script works fine with users less than 1000. I tried to feed the script with a csv file with 7125 user addresses, and now I'm getting the error below. How can i resolve this, so that it does not fail.

I tried to use -resultsize unlimited switch but it fails. I believe its a throttling problem.

My script

$All = Import-CSV "C:\Usersfile.csv"
$All | ForEach-Object {

    $mailbox = $_.UserPrincipalName
    $quota = Get-Mailbox -identity $mailbox | select ProhibitSendQuota, ProhibitSendReceiveQuota, RecoverableItemsQuota, ArchiveQuota, ArchiveWarningQuota

    $CurrentMailboxUsedSize = Get-MailboxStatistics -identity $mailbox | select TotalItemSize

    $recoverableitem = Get-MailboxFolderStatistics -identity $mailbox | ? {$_.Name -eq "Recoverable Items"}


    New-Object -TypeName PSObject -Property @{

        UserPrincipalName = $mailbox
        CurrentMailboxUsedSize = $CurrentMailboxUsedSize.TotalItemSize
        ProhibitSendQuota = $quota.ProhibitSendQuota
        MailboxTotalSize = $quota.ProhibitSendReceiveQuota
        CurrentRecoverableItemsSize = $recoverableitem.FolderAndSubfolderSize
        RecoverableItemsQuota = $quota.RecoverableItemsQuota        
        ArchiveMailboxQuota = $quota.ArchiveQuota
 ArchiveWarningQuota = $quota.ArchiveWarningQuota

    } 

} | Sort-Object 'UserPrincipalName', 'CurrentMailboxUsedSize', 'CurrentRecoverableItemsSize', 'ProhibitSendQuota', 'MailboxTotalSize', 'RecoverableItemsQuota', 'ArchiveMailboxQuota', 'ArchiveWarningQuota' | Export-CSV "C:\Users\Admin\Desktop\results.csv" -NoTypeInformation -Encoding UTF8

The error that i'm getting on 7125 users.
`
WARNING: By default, only the first 1000 items are returned. Use the ResultSize parameter to specify the number of items returned. To return

all items, specify "-ResultSize Unlimited". Be aware that, depending on the actual number of items, returning all items can take a long time

and consume a large amount of memory. Also, we don't recommend storing the results in a variable. Instead, pipe the results to another task o

r script to perform batch changes.

Cannot bind argument to parameter 'Identity' because it is null.

  • CategoryInfo : InvalidData: (:) [Get-MailboxStatistics], ParameterBindingValidationException
  • FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Get-MailboxStatistics
  • PSComputerName : outlook.office365.com
    `
Microsoft Exchange Online Management
Microsoft Exchange Online Management
Microsoft Exchange Online: A Microsoft email and calendaring hosted service.Management: The act or process of organizing, handling, directing or controlling something.
4,192 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,381 questions
0 comments No comments
{count} votes

Accepted answer
  1. Martin Tästensen 456 Reputation points
    2022-04-13T10:35:30.113+00:00

    So, first things first. get-mailboxstatistics is notoriously slow, i have been using it on customers with 16.000 mailboxes, and that script will take forever to complete. So please use with caution.

    If i were to make it like you did, i would do it like below. If i read the error message correctly (the last part of it), it seems to have issues finding the identity of the user. I usually never pipe directly to a foreach, and as such can't tell exactly why it fails, but the below code should fix it.

    $All = Import-CSV "C:\temp\userfiles.csv"
    $exportpath = "C:\temp\exportfile.csv"
    foreach($i in $All)
    {
    
         $mailbox = $i.UserPrincipalName
         $quota = Get-Mailbox -identity $mailbox | select ProhibitSendQuota, ProhibitSendReceiveQuota, RecoverableItemsQuota, ArchiveQuota, ArchiveWarningQuota
    
         $CurrentMailboxUsedSize = Get-MailboxStatistics -identity $mailbox | select TotalItemSize
    
         $recoverableitem = Get-MailboxFolderStatistics -identity $mailbox | ? {$_.Name -eq "Recoverable Items"}
    
         $p = @{
             UserPrincipalName = $mailbox
             CurrentMailboxUsedSize = $CurrentMailboxUsedSize.TotalItemSize
             ProhibitSendQuota = $quota.ProhibitSendQuota
             MailboxTotalSize = $quota.ProhibitSendReceiveQuota
             CurrentRecoverableItemsSize = $recoverableitem.FolderAndSubfolderSize
             RecoverableItemsQuota = $quota.RecoverableItemsQuota        
             ArchiveMailboxQuota = $quota.ArchiveQuota
             ArchiveWarningQuota = $quota.ArchiveWarningQuota
         }
        $objcmddata = New-Object -TypeName psobject -Property $p
        $objcmddata | select-object | Sort-Object 'UserPrincipalName', 'CurrentMailboxUsedSize', 'CurrentRecoverableItemsSize', 'ProhibitSendQuota', 'MailboxTotalSize', 'RecoverableItemsQuota', 'ArchiveMailboxQuota', 'ArchiveWarningQuota' | Export-CSV $exportpath -NoTypeInformation -Encoding UTF8 -Delimiter ';' -Append
     } 
    
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. KyleXu-MSFT 26,211 Reputation points
    2022-04-14T03:20:03.993+00:00

    @Benard Mwanza

    You can also have a try with this one:

    $Mailboxes = Import-CSV "C:\Usersfile.csv"      
    $data = @()  
      
    foreach ($Mailbox in $mailboxes){  
        $quota = Get-Mailbox -identity $Mailbox.UserPrincipalName | select UserPrincipalName,ProhibitSendQuota, ProhibitSendReceiveQuota, RecoverableItemsQuota, ArchiveQuota, ArchiveWarningQuota, TotalItemSize,RecoverableSize  
        $quota.TotalItemSize = (Get-MailboxStatistics -Identity $Mailbox.UserPrincipalName).TotalItemSize.value  
        $quota.RecoverableSize = (Get-MailboxFolderStatistics -Identity $Mailbox.UserPrincipalName | where{$_.FolderPath -like "*Recoverable*"}).FolderAndSubfolderSize  
        $data+=$quota  
    }  
    $data | Export-csv C:\Users\Admin\Desktop\results.csv -NoTypeInformation  
    

    This blog may also be useful to you: Updated: Running PowerShell cmdlets for large numbers of users in Office 365


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


    0 comments No comments