DCSIMG
Mapping WMI mailbox object to its AD user account - Shay Levy

Shay Levy

If you repeat it, PowerShell it!

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

Comments

Phil23 said:

Hi, great article.

the only remaining issue for me is we have single forest but 3 domains. the results only return accounts to the domain I am currently logged into. is there a way to make this work across all three domains.

Thanks for getting me this far with powershell.

PHil23

# February 10, 2010 9:38 PM

ScriptFanatic said:

With WMI you can use the -Credential parameter to specify different credentials per domain (e.g -Credential DOMAIN\User). For Quest cmdlets take a look at the help for Connect-QADService.

# February 11, 2010 3:48 PM

Phil23 said:

So I would run the command three times with diff credentials and then merge the three tables together. I am new to powershell. can that be done in the same command by using the piping feature. or run the three seperately and then merge the tables to one table.

Thanks,

# February 11, 2010 11:54 PM

ScriptFanatic said:

See if this works for you:

$ConnectionAccount1 = "domain1\administrator"

$ConnectionAccount2 = "domain2\administrator"

$ConnectionAccount3 = "domain3\administrator"

$domain1WMICreds = Get-Credential $ConnectionAccount1

$domain2WMICreds = Get-Credential $ConnectionAccount2

$domain3WMICreds = Get-Credential $ConnectionAccount3

$domain1QADCreds = Read-host "Enter credetials for domain 1" -AsSecureString

$domain2QADCreds = Read-host "Enter credetials for domain 2" -AsSecureString

$domain3QADCreds = Read-host "Enter credetials for domain 3" -AsSecureString

$DCs = "DC1.Domain1.com","DC2.Domain1.com","DC3.Domain1.com"

$Exchange = "Server1","Server2","Server3"

$WMICreds = $domain1Creds,$domain2Creds,$domain3Creds

$QADCreds = $domain1QADCreds,$domain2QADCreds,$domain3QADCreds

$ConnectionAccounts = $ConnectionAccount1,$ConnectionAccount2,$ConnectionAccount3

for($i=0; $i -lt 3; $i++)

{

Get-WmiObject -Class Exchange_Mailbox -NameSpace root\microsoftexchangev2 -Credential $WMICreds[$i] -ComputerName $Exchange[$i] | Sort-Object MailboxDisplayName | Select-Object MailboxDisplayName, @{Name='SamAccountName';Expression={(Get-QADUser -Service $DCs[$i] -ConnectionAccount $ConnectionAccounts[$i] -ConnectionPassword $QADCreds[$i] -Identity $_.LegacyDN).SamAccountName}}, ServerName, StorageGroupName, StoreName, Size

}

# February 12, 2010 9:54 AM

Mapping WMI mailbox object to its AD user account (the “right” way) « Slipsec said:

Pingback from  Mapping WMI mailbox object to its AD user account (the “right” way) «  Slipsec

# May 26, 2011 5:06 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 


Enter the numbers above: