Categories

The inverse association

In my last post I showed how to relate find the groups of which a local user is a member. A comment was left asking about the inverse relationship.

In this case we can just turn the code round.

Get-CimInstance -ClassName Win32_Group |
foreach {
$users = Get-CimAssociatedInstance -InputObject $psitem `
-ResultClassName Win32_UserAccount |
select -ExpandProperty Caption

$psitem | Add-Member -MemberType NoteProperty -Name 'Users' `
-Value ($users -join ";") -PassThru

} | select Name, Users

 

Note: You can’t do this as a matter of course with all WMI relationships. Some relationships are one way in that A has a link to B but B has no way to link back to A unless you can find a property in common.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>