Fun With Custom PSObjects

I was recently asked to generate a report on mailbox usage by a specific subset of AEMS users.  The tricky part is that the various pieces of info that I need are returned by two distinct Exchange Management Shell commands.  In order to merge properties from two different returned objects, I found it expedient to tack the values from those properties into a new PSObject:

#initializing an array to hold the constructed PSObjects
[array]$users = $null          

#  iterate through all members of myGroup
Get-DistributionGroupMember “myGroup” | ForEach-Object {    
# some info needed from Get-Mailbox
$mb=get-mailbox  $_.samaccountname    
# some from Get-MailboxStatistics
$mbstats = get-mailboxstatistics $_.samaccountname   

# create PSObject into which we will merge results
$user = New-Object PSObject   

# Now we add the desired properties into the PSObject
$user | Add-Member NoteProperty Name $mb.DisplayName
$user | Add-Member NoteProperty Usage $mbstats.TotalItemSize.Value.ToMB() 
$user | Add-Member NoteProperty Quota $mb.ProhibitSendQuota
$user | Add-Member NoteProperty Account $mb.SamAccountName 

# skipping users for whom no data is returned because they’ve never logged in
if ($mb.DisplayName -ne $NULL){$users += $user}

}

# Now, export the report as a CSV file:
$users | export-csv -path .\myGroup-Users.csv

Leave a Reply