I want to find all users on a particular storage group... part 2

Careful readers of my blog might have noticed that in my last post I alluded to some more detail on “drilling into the Database property directly”. Although I put together a couple of useful iterative ways to get to the filtered data they had asked for, some might still wonder… is there no way to get to this data through the mailbox object directly?

Well, such careful and astute readers would be correct in their insistence that it must be possible!

The original one-liner the questioner had sent to me was like this:

Get-Mailbox | where { $_.Database.StorageGroupName -eq ‘First Storage Group’ }

What they were trying to do was to drill two levels into the output object of Get-Mailbox… they wanted to filter not on the database property of the mailbox, but on some property of the database property, where the database in the mailbox properties was taken as an object. Pretty useful stuff if you can do it, right?

How about this one-liner?

Get-Mailbox | where { $_.Database.parent.name -eq ‘First Storage Group’ }

So, why does this one work and how did I come up with it? Let’s take these one at a time.

Why does it work?

It works because we’re able to use some knowledge of the database & storage group relationship… that the database is the child of the storage group. So if we have a database object, we can know that it has one parent and that this parent object will be a storage group.

Ok then, so how did you come up with the specific syntax?

This is another fun “spelunking in powershell” exercise.

First, I took a look at the Get-Mailbox output, and drilled into the Database property a bit:

$a = Get-Mailbox
$a.Database | Get-Member

Which results in this view (which I’ve cropped a bit here for space):

   TypeName: Microsoft.Exchange.Data.Directory.ADObjectId

Name MemberType Definition
---- ---------- ----------
<cut out a bunch of properties>
Name Property System.String Name {get;}
ObjectGuid Property System.Guid ObjectGuid {get;}
Parent Property Microsoft.Exchange.Data.Directory.ADObjectI...
Rdn Property Microsoft.Exchange.Data.Directory.AdName Rd...

This tells me that the Database property is actually an ADObjectID (see TypeName at the top) and that it has a “parent” property which is also an ADObjectID. Let’s confirm that this really is what we want it to be:

[PS] C:\>$a.database.parent

Rdn : CN=First Storage Group
Parent : InformationStore
Depth : 11
DistinguishedName : CN=First Storage Group,CN=InformationStore,CN=E12,CN=Server
s,CN=Exchange Administrative Group (FYDIBOHF23SPDLT),CN=Adm
inistrative Groups,CN=first organization,CN=Microsoft Excha
DomainId : e12dom.local
ObjectGuid : 00000000-0000-0000-0000-000000000000
Name : First Storage Group

Score! Now we just have to put it all together and end up with a one-liner like:

Get-Mailbox | where { $_.Database.parent.name -eq ‘First Storage Group’ }