Forum Discussion
Ps session disconnecting
- Feb 27, 2023Could be the delimiter, I choose ' ; ' but perhaps it's ' , ' for you? And that's pretty slow. It is a lot of mailboxes of course, and how much RAM is PowerShell using in Task Manager during that?
Instead of putting everything in one $report table, you could also export it to one csv with the append parameter. That way it will not consume that much memory
- AustinSundarFeb 23, 2023Copper Contributor
Harm_Veenstra i am not an expert in PS, but tried something like below. however, it failed after getting information of 8500 mailboxes. i also see the error below
Could you help to correct the mistakes in the script? or guide me
PS Error
$report = @() $Mbxs = Get-Mailbox -ResultSize unlimited -RecipientTypeDetails UserMailbox, SharedMailbox foreach ($Mbx in $Mbxs) { $TotalItemSize = (Get-MailboxStatistics $Mbx.UserPrincipalName).TotalItemSize $ItemCount = (Get-MailboxStatistics $Mbx.UserPrincipalName).ItemCount $TotalDeletedItemSize = (Get-MailboxStatistics $Mbx.UserPrincipalName).TotalDeletedItemSize $ArchiveTotalItemSize = (Get-MailboxStatistics $Mbx.UserPrincipalName -Archive -WarningAction SilentlyContinue).TotalItemSize $ArchiveItemCount = (Get-MailboxStatistics $Mbx.UserPrincipalName -Archive -WarningAction SilentlyContinue).ItemCount $ArchiveTotalDeletedItemSize = (Get-MailboxStatistics $Mbx.UserPrincipalName -Archive -WarningAction SilentlyContinue).TotalDeletedItemSize #$email= Get-Mailbox $mbx | select @{Name='EmailAddresses'; Expression={$_.EmailAddresses -join ","}} $report = $null $reportObj = New-Object PSObject $reportObj | Add-Member NoteProperty -Name "DisplayName" -Value $mbx.DisplayName $reportObj | Add-Member NoteProperty -Name "RecipientTypeDetails" -Value $mbx.RecipientTypeDetails $reportObj | Add-Member NoteProperty -Name "PrimarySmtpAddress" -Value $mbx.PrimarySmtpAddress $reportObj | Add-Member NoteProperty -Name "EmailAddresses" -Value $mbx.EmailAddresses $reportObj | Add-Member NoteProperty -Name "RetentionPolicy" -Value $mbx.RetentionPolicy $reportObj | Add-Member NoteProperty -Name "ArchiveName" -Value $mbx.ArchiveName $reportObj | Add-Member NoteProperty -Name "AutoExpandingArchiveEnabled" -Value $mbx.AutoExpandingArchiveEnabled $reportObj | Add-Member NoteProperty -Name "TotalItemSize" -Value $TotalItemSize $reportObj | Add-Member NoteProperty -Name "ItemCount" -Value $ItemCount $reportObj | Add-Member NoteProperty -Name "TotalDeletedItemSize" -Value $TotalDeletedItemSize $reportObj | Add-Member NoteProperty -Name "ArchiveTotalItemSize" -Value $ArchiveTotalItemSize $reportObj | Add-Member NoteProperty -Name "ArchiveItemCount" -Value $ArchiveItemCount $reportObj | Add-Member NoteProperty -Name "ArchiveTotalDeletedItemSize" -Value $ArchiveTotalDeletedItemSize $report += $reportObj $report|Export-Csv "" -Append -NoTypeInformation } $report- Feb 23, 2023
AustinSundar You did export it to csv, but you also keep adding stuff to the $report variable making it bigger and bigger 😉 While testing it against my test tenant, I changed it to avoid errors when users have no archive. I also changed to customobject to something smaller and readable and added a counter:)
Could you test this one below and change the location to the csv file in the $reportpath variable to something else that matches your temp or data folder?
$reportpath='d:\temp\mailboxes.csv' $Mbxs = Get-Mailbox -ResultSize unlimited -RecipientTypeDetails UserMailbox, SharedMailbox $counter=1 foreach ($Mbx in $Mbxs) { Write-Host ("Checking mailbox {0}/{1}" -f $counter, $mbxs.count) $TotalItemSize = (Get-MailboxStatistics $Mbx.UserPrincipalName).TotalItemSize $ItemCount = (Get-MailboxStatistics $Mbx.UserPrincipalName).ItemCount $TotalDeletedItemSize = (Get-MailboxStatistics $Mbx.UserPrincipalName).TotalDeletedItemSize if ($null -ne $mbx.ArchiveDatabase) { $ArchiveName = $mbx.ArchiveName $ArchiveTotalItemSize = (Get-MailboxStatistics $Mbx.UserPrincipalName -Archive -WarningAction SilentlyContinue).TotalItemSize $ArchiveItemCount = (Get-MailboxStatistics $Mbx.UserPrincipalName -Archive -WarningAction SilentlyContinue).ItemCount $ArchiveTotalDeletedItemSize = (Get-MailboxStatistics $Mbx.UserPrincipalName -Archive -WarningAction SilentlyContinue).TotalDeletedItemSize } else { $ArchiveName = 'Not applicable' $ArchiveTotalItemSize = 0 $ArchiveItemCount = 0 $ArchiveTotalDeletedItemSize = 0 } $report = [PSCustomObject]@{ "DisplayName" = $mbx.DisplayName "RecipientTypeDetails" = $mbx.RecipientTypeDetails "PrimarySmtpAddress" = $mbx.PrimarySmtpAddress "EmailAddresses" = $mbx.EmailAddresses "RetentionPolicy" = $mbx.RetentionPolicy "TotalItemSize" = $TotalItemSize "ItemCount" = $ItemCount "ArchiveName" = $ArchiveName "AutoExpandingArchiveEnabled" = $mbx.AutoExpandingArchiveEnabled "TotalDeletedItemSize" = $TotalDeletedItemSize "ArchiveTotalItemSize" = $ArchiveTotalItemSize "ArchiveItemCount" = $ArchiveItemCount "ArchiveTotalDeletedItemSize" = $ArchiveTotalDeletedItemSize } $report | Export-Csv -Path $reportpath -Append -NoTypeInformation -Encoding UTF8 -Delimiter ';' $counter++ }My output is like this:
- AustinSundarFeb 27, 2023Copper Contributor
Harm_Veenstra i tried with less than 20 mailboxes. i am getting the output, but the format seems to be not correct .may be i need to check formatting in excel.
But when i tried to query all mailboxes, it seems be very slow to get the output. not sure if this is because of querying multiple items or number of mailboxes.
in 5hrs it queried only 2560 mailboxes