Mapping WMI mailbox object to its AD user account
Few days ago I got a request from a reader of my blog:
"I found the WMI commands to get info from Exchange 2003 servers and the following gets me the displayname, servername, storagegroupname, and storename but am I able to get the SamAccountName? Do you know any way?"
Get-WmiObject -Class Exchange_Mailbox -NameSpace root\microsoftexchangev2 -ComputerName ExchangeServer | Sort-Object MailboxDisplayName | Format-Table MailboxDisplayName, ServerName, StorageGroupName, StoreName, Size -auto
First, lets see how a sample mailbox output looks like when we issue the above WMI command (without piping to sort and format-table):
(...)
AssocContentCount : 159
Caption :
DateDiscoveredAbsentInDS :
DeletedMessageSizeExtended : 0
Description :
InstallDate :
LastLoggedOnUserAccount : Domain\User1
LastLogoffTime : 20090716113423.000866+***
LastLogonTime : 20090716113027.000840+***
LegacyDN : /O=MyOrg/OU=domain.com/CN=RECIPIENTS/CN=USER1
MailboxDisplayName : User1
MailboxGUID : {AB7AE3FC-0FD4-47C1-841C-5EA0857F8093}
Name :
ServerName : EX1
Size : 634673
Status :
StorageGroupName : Second Storage Group
StorageLimitInfo : 8
StoreName : Second Storage Group (EX1)
TotalItems : 33580
We can map each mailbox to its AD user object using one of the following properties:
1. LastLoggedOnUserAccount
2. LegacyDN
3. MailboxDisplayName
4. MailboxGUID
I wouldn't count much on the first one, not on my environment anyway. We have a special user account that archives all mailboxes each night so the mailbox is stamped with that user instead of the mailbox owner user name. I would also skip option #3, I'm not sure if MailboxDisplayName is in sync with the user DisplayName AD attribute. I choosed to use LegacyDN since MailboxGUID needs to be formatted first (remove the hyphens and curly braces).
OK, with Quest's Get-QADUser cmdlet we can get each user by binding LegacyDN to the Identity parameter and introducing the result as additional property (column) to Format-Table using a calculated property:
Get-WmiObject -Class Exchange_Mailbox -NameSpace root\microsoftexchangev2 -ComputerName ExchangeServer | Sort-Object MailboxDisplayName | Format-Table MailboxDisplayName, @{Label='SamAccountName';Expression={(Get-QADUser -Identity $_.LegacyDN).SamAccountName}}, ServerName, StorageGroupName, StoreName, Size -auto
MailboxDisplayName SamAccountName ServerName StorageGroupName StoreName Size
------------------ -------------- ---------- ---------------- --------- ----
User1 User1 EX1 First Storage Group Store1 (EX1) 103546
User2 User2 EX1 Second Storage Group Store2 (EX1) 16621
User3 User3 EX1 Third Storage Group Store3 (EX1) 64663