powershell - Sort LastLogonTime -Descending Gives Identity Error - Stack Overflow

admin2025-04-29  2

I am trying to sort LastLogonTime by descending order for users in my Exchange Online platform.

Pulling up the users comes through just fine showing their display name and last logon time with the below code.

Get-Mailbox -ResultSize Unlimited | Get-MailboxStatistics | Select-Object DisplayName, LastLogonTime

When I add the sort command:

Get-Mailbox -ResultSize Unlimited | Get-MailboxStatistics | Select-Object DisplayName, LastLogonTime | Sort LastLogonTime -Descending

I am given this error on all accounts:

Get-MailboxStatistics: Ex838C9A|Microsoft.Exchange.Configuration.Tasks.ManagementObjectAmbiguousException|The specified mailbox Identity:"sales"  isn't
unique.

Not sure what is going on since the users are pulled just fine without the sort command. Any suggestions?

I tried to sort lastlogontime by using the line, expecting the results to print in descending order of time:

Get-Mailbox -ResultSize Unlimited | Get-MailboxStatistics | Select-Object DisplayName, LastLogonTime | Sort LastLogonTime -Descending

I am trying to sort LastLogonTime by descending order for users in my Exchange Online platform.

Pulling up the users comes through just fine showing their display name and last logon time with the below code.

Get-Mailbox -ResultSize Unlimited | Get-MailboxStatistics | Select-Object DisplayName, LastLogonTime

When I add the sort command:

Get-Mailbox -ResultSize Unlimited | Get-MailboxStatistics | Select-Object DisplayName, LastLogonTime | Sort LastLogonTime -Descending

I am given this error on all accounts:

Get-MailboxStatistics: Ex838C9A|Microsoft.Exchange.Configuration.Tasks.ManagementObjectAmbiguousException|The specified mailbox Identity:"sales"  isn't
unique.

Not sure what is going on since the users are pulled just fine without the sort command. Any suggestions?

I tried to sort lastlogontime by using the line, expecting the results to print in descending order of time:

Get-Mailbox -ResultSize Unlimited | Get-MailboxStatistics | Select-Object DisplayName, LastLogonTime | Sort LastLogonTime -Descending
Share Improve this question edited Jan 6 at 23:55 mklement0 444k68 gold badges712 silver badges930 bronze badges asked Jan 6 at 23:54 fr0stbyt3fr0stbyt3 211 silver badge1 bronze badge 6
  • Strange... What happens when you do this in two steps: $Results = Get-Mailbox -ResultSize Unlimited | Get-MailboxStatistics | Select-Object DisplayName, LastLogonTime; $Results | Sort LastLogonTime -Descending ? – iRon Commented Jan 7 at 7:29
  • sort-object blocks until it has every item. Maybe get-mailboxstatistics doesn't like that. You can also sort before the select. – js2010 Commented Jan 7 at 13:56
  • @iRon unfortunately, the same error is given back. – fr0stbyt3 Commented Jan 7 at 21:51
  • @js2010 Thank you for the suggestion. Tried that and still comes up with the same error. – fr0stbyt3 Commented Jan 7 at 21:54
  • "the same error is given back", at the first step or the second step? The comments here are troubleshooting hints (not direct solutions). Other steps you might consider to narrow down the issue and which might lead to a better understanding of what happens and might lead to a possibly solution: $Results = Get-Mailbox -ResultSize Unlimited | Get-MailboxStatistics | Select-Object DisplayName, LastLogonTime -first 3 | Export-Csv .\Temp.Csv; Import-Csv .\Temp.Csv | Sort LastLogonTime -Descending. – iRon Commented Jan 8 at 10:35
 |  Show 1 more comment

1 Answer 1

Reset to default 1

From trial and error I have been able to identify the following;

The likely cause is that multiple mailboxes have the same alias, this can be found by trialing the following command; (You would identify the offending mailbox from the error specified.)

Get-Mailbox sales

If this yields multiple results, you can then trial the following to confirm that this is what is causing the error by using the following command;

Get-MailboxStatistics sales

This should throw the same error as you had before, the reason why you may have not of seen this when removing the sort command is that it will provide the results for valid mailbox identities and throw the errors between these responses.

There is a couple of work around's to this issue;

1, Updating the alias on mailboxes that have duplicates, if synchronized from local AD to office 365 you will need to update the mailNickname in active directory to facilitate this.

2, Adapt the script to allow the pipeline to use a different value;

Get-Mailbox -ResultSize Unlimited | Select -expand UserPrincipalName | Get-MailboxStatistics | Select-Object DisplayName, LastLogonTime | Sort LastLogonTime -Descending
转载请注明原文地址:http://anycun.com/QandA/1745938511a91386.html