PowerShell Donut

I’m creating a PowerShell donut that shows the state of Health Service Watchers (SCOM) from nested groups in a donut. The query returns 124 objects, each with a unique id (just a counter in the script). The response data also shows 124 objects. However, when I render the data as a donut, it shows 7750 objects. Script is below:

$Group = Get-scomgroup -DisplayName "<Group of nested groups>"
$Members = $Group.GetRelatedMonitoringObjects()

$sgmembers = @()

ForEach($Member in $Members){

    $subgroup = $Member.DisplayName

    $servergroup = Get-SCOMGroup -DisplayName "$subgroup"
    $sgmembers += $servergroup.GetRelatedMonitoringObjects() | Select-Object HealthState, @{N='name';E={$_.DisplayName}}
}
$sgmembers | Add-Member -MemberType NoteProperty "id" -Value 0
$sgmembers | Add-Member -MemberType NoteProperty "state" -Value '0'
$counter = 0
ForEach ($object in $sgmembers){

    $counter++
    $object.id= $counter
    $object.state = if ($object.HealthState = 'Success'){'healthy'}
                elseif ($object.HealthState = 'Warning'){'unhealthy'}
                elseif ($object.HealthState = 'Error'){'critical'}
                elseif ($object.HealthState = 'Uninitialized'){'unknown'}
}
$sgmembers

I’m using id for the value (the only one available to select) and state for the grouping. Any tips? The documentation and examples for this are not awesome, and there is no example i can find on GitHub.

I don’t have the SquaredUp website installed but it’s likely that you can’t use “id” for your own purposes. The dashboard likely depends on the true object Id. Try this.

$Group = Get-SCOMGroup -DisplayName "<Group of nested groups>"
$Members = $Group.GetRelatedMonitoringObjects()

[System.Collections.ArrayList]$sgmembers = @()

ForEach($Member in $Members)
{
  $subgroup = $Member.DisplayName
  $servergroup = Get-SCOMGroup -DisplayName "$subgroup"
  $servergroup.GetRelatedMonitoringObjects() | ForEach-Object -Process {
    $Null = $sgmembers.Add(($_ | Select-Object -Property HealthState, @{
          N = 'Name'
          E = {
            $_.DisplayName
          }
    }, Id, 'State', 'Counter' ))
  }
}

$counter = 0
$hash = @{
  'Success'     = 'healthy'
  'Warning'     = 'unhealthy'
  'Error'       = 'critical'
  'Uninitialized' = 'unknown'
}

ForEach ($object in $sgmembers)
{
  $counter++
  $object.Counter = $counter
  $object.State = $hash["$($object.HealthState)"]
}
$sgmembers

I was able to get this resolved via SquaredUp tech support. As near as I can tell, ID is not a unique value for each object returned, as I had assumed. It’s the same for all as a group. Counter doesn’t even appear to be necessary, I could just programmatically set them all to 1 and it would work as expected. Thanks, and I didn’t know you were lurking here, Tyson. I employ a lot of your stuff!