SOLUTION: Reporting Exchange 2010/2007 Mailboxes that has last logon older than 90 days. (Error: Allowed maximum is 524288000)

Applies to Exchange 2010 and Exchange 2007 mailboxes and Windows 2008 R2 sp1

We want to list all mailboxes that has last logon time older than 90 days. At first time, the following command or similar comes to your mind.

Get-MailboxStatistics | where {$_.Lastlogontime -lt (get-date).AddDays(-90)} | Select displayName,LastLoggedOnUserAccount,LastLogonTime |Export-Csv c:\Lastlogontime.csv

it works for small domain (about 1000 users). But if I run it in a large domain (more than 10000 users) I got the following errors.

Sending data to a remote command failed with the following error message: Exception of type ‘System.OutOfMemoryExceptio
n’ was thrown. For more information, see the about_Remote_Troubleshooting Help topic.
+ CategoryInfo : OperationStopped: (System.Manageme…pressionSyncJob:PSInvokeExpressionSyncJob) [], PSRe
+ FullyQualifiedErrorId : JobFailure

Invoke-Command : Cannot write input as there are no more running pipelines
At C:\Users\admin\AppData\Roaming\Microsoft\Exchange\RemotePowerShell\\Exccas1.internal.contoso.compsm1:15619 char:29
+ $scriptCmd = { & <<<< $script:InvokeCommand `
+ CategoryInfo : InvalidOperation: (:) [Invoke-Command], PSInvalidOperationException
+ FullyQualifiedErrorId : NoMoreInputWrite,Microsoft.PowerShell.Commands.InvokeCommandCommand


The total data received from the remote client exceeded allowed maximum. Allowed maximum is 524288000

After hours of research I found some IIS and powershell limits but they were all useless. The problem is we need to append csv file not a bulk operation. So we need for each loop.

I modified the script and made it simple in and run on Exchange 2010 Management Shell in Windows 2008 R2 sp1 by adding little IF statement. With this script, csv file is created and data is appended line by line. In the above command it was a bulk pipe operation and created 0 KB csv file, and updated it at the end of the session and raised errors.

copy and save it as lastlogontime.ps1.

$Days = 90
$TimeLimit = (Get-Date).AddDays(-$Days)
$Mailboxes = Get-Mailbox -domaincontroller EXCCAS1.INTERNAL.CONTOSO.COM -ResultSize Unlimited
$Mailboxes | foreach {
$stats = Get-MailboxStatistics $_
If ($stats.LastLogonTime -lt $TimeLimit) { $stats | Select DisplayName, ItemCount,
LastLogonTime, LastLoggedOnUserAccount }
} | Export-CSV -NoTypeInformation c:\LastLogonTime.csv

($stats.LastLogonTime -lt $TimeLimit) returns true if lastlogontime is less than 90 and adds the user to the csv file. It takes about 2 hours in 20,000 user domain. But you can observe the csv file during run time and open it as read only to see the progress.

More Information:
In multi domain forests, all you need to change domaincontroller name of the child domain.

Thanks to Jonas Andersson for the original script.

Gallery | This entry was posted in Exchange Server and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s