Techibee.com

PowerShell: Get SCOM groups of a computer account

In SCOM there is no straight forward way to list all the groups of a given computer object. In last few days I worked extensively on SCOM and hit with this requirement of knowing the groups to which a computer account belongs. We can use some SQL queries to list the groups but I felt having a powershell code will be much more useful and wrote the below function.

You need to make sure that you are executing this script from SCOM operator shell. Otherwise it will fail as it is using SCOM PS cmdlets. We can make the script from a normal PS windows as well but that needs some more effort which I want to put in at later stage.

Another good thing with this script is, it fetches the nested groups as well.

Code:

function Get-GroupNames {
[cmdletbinding()]
param($computerFQDN)
$containmentRel = Get-RelationshipClass -name:’Microsoft.SystemCenter.InstanceGroupContainsEntities’
$computerClass = Get-MonitoringClass -name:”Microsoft.Windows.Computer”
$criteria = [string]::Format(“PrincipalName = ‘{0}'”,$computerFQDN)
try {
$computer = Get-MonitoringObject -monitoringClass:$computerClass -criteria:$criteria
$relatedObjects = $computer.GetMonitoringRelationshipObjectsWhereTarget($containmentRel,[Microsoft.EnterpriseManagement.Configuration.DerivedClassTraversalDepth]::Recursive,[Microsoft.EnterpriseManagement.Common.TraversalDepth]::Recursive)
}
catch {
$_
write-host “An error occurred while querying groups of $computerFQDN”
}

foreach($group in $relatedObjects)
{
[array]$Groups = $groups + $group.SourceMonitoringObject.DisplayName
}
if($groups) {
return $groups
} else {
write-host “No groups available for $computerFQDN”
}
}

Usage:


Get-GroupName -ComputerFQDN myserver1

Hope this helps….

Exit mobile version