listing the group membership of a computer in opsmgr [part 2]

yesterday, i posted an entry about retrieving a computer’s membership through a very backwards way that i cobbled together.  after talking to pete zerger for a little while, i started poking into how to make boris’ script work.  it was initially only pulling back two groups for me.

well, i managed to increase that count to six.  however, my output and boris’ still doesn’t match up.  it could be an incorrect root class, i’m using.  either way, i wanted to post it to see if you guys could direct me to a better solution.  who knows?

since you can’t seem to use an abstract class directly in boris’ script, i modified it a bit to first get the abstract class object using get-monitoringobject and then pull out the objects into an array.  afterwards, that array is fed into the get-monitoringclass cmdlet.  at that point, we should have a pretty good set of objects we can use.

those objects are sent back down the pipe to get-monitoringobject using the criteria of $computerFQDN to create a new array called $subClasses.  that array is sent down the pipe to finally run the lines in boris’ script that handles getting the related objects of $computerFQDN.

take a look over it if you have a chance, and let me know what you see.  here’s some sample output of a domain controller using my script and the edited boris script:

my script -

Agent Managed Computer Group
All Windows Computers
Windows Server 2003 Computer Group
Windows Server Computer Group
Windows Server Instances Group

 

edited boris script -

AD Domain Controller Group (Windows 2003 Server)
Windows Server Instances Group
Windows Server Instances Only Group

 

so you see?  holes.  everywhere.  more playing to do later.  here’s the script:

param($computerFQDN)

$ErrorActionPreference = "SilentlyContinue"

function GetGroupNames($computerFQDN)
{
$containmentRel = Get-RelationshipClass -name:'Microsoft.SystemCenter.InstanceGroupContainsEntities'
$abstractClass = Get-MonitoringClass -name:"Microsoft.Windows.ComputerRole"

$subObjects = Get-MonitoringObject -monitoringClass:$abstractClass
$subObjects = $subObjects | ForEach-Object {
$_.FullName.Split(":")[0]
} | Sort-Object -Unique

$subObjects | ForEach-Object {
$subObjects += Get-MonitoringClass -name:$_
}

$criteria = [string]::Format("Path = '{0}'",$computerFQDN)

$subObjects | ForEach-Object {
$subClasses += Get-MonitoringObject -MonitoringClass:$_ -Criteria:$criteria
}

$subClasses | ForEach-Object {
$relatedObjects += $_.GetMonitoringRelationshipObjectsWhereTarget($containmentRel,`
[Microsoft.EnterpriseManagement.Configuration.DerivedClassTraversalDepth]::Recursive,`
[Microsoft.EnterpriseManagement.Common.TraversalDepth]::Recursive)
}

foreach($group in ($relatedObjects | Sort-Object SourceMonitoringObject -Unique))
{
$group.SourceMonitoringObject.DisplayName
}

}

GetGroupNames $computerFQDN

Comments