question

jg-4093 avatar image
0 Votes"
jg-4093 asked StoyanChalakov commented

Find empty SCOM groups

Hi!


Is there any way through Powershell to find empty SCOM groups easily? With empty i mean with literally zero group members.

msc-operations-manager
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

StoyanChalakov avatar image
0 Votes"
StoyanChalakov answered jg-4093 commented

Hey,

I have put a quick script together, tested it and it will report all your empty groups in green. Please try it out:

 $Groups = Get-SCOMGroup 
 foreach ($Group in $Groups) {
 if (!($Group.GetRelatedMonitoringObjects())) {
 Write-Host "The group $($Group.DisplayName) is empty" -ForegroundColor Green
 } else {
 Write-Host "The group $($Group.DisplayName)is NOT empty" -ForegroundColor Red
 }
 }

I will very much appreciate your feedback!


(If the reply was helpful please don't forget to upvote or accept as answer, thank you)
Regards,
Stoyan

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi!


Works great! Is it possible to sort them on color as well? Thanks alot!

0 Votes 0 ·
StoyanChalakov avatar image
1 Vote"
StoyanChalakov answered StoyanChalakov edited

Hi,

sure..The code below will loop over all groups, show you if the group is empty or not (Red or Green) while collecting the data and dump (output) in white only the empty groups at the end:

  #Create an array to store the empty groups
  $AllEmptyGroups = @() 
  #Get all SCOM groups
  $Groups = Get-SCOMGroup 
     
  #Check if the group has members or not
  foreach ($Group in $Groups) {
  if (!($Group.GetRelatedMonitoringObjects())) {
  #Group Display Name
  $CurrentEmptyGroup = $Group.DisplayName
  Write-Host "The group $($Group.DisplayName) is empty" -ForegroundColor Green
  #Add the current empty group to the collection
  $AllEmptyGroups += $CurrentEmptyGroup
  } else {
  Write-Host "The group $($Group.DisplayName)is NOT empty" -ForegroundColor Red
  }
  }
  #Output all empty groups
  $AllEmptyGroups

If you don't want to display green or red while collecting the list ($AllEmptyGroups) you can just comment out the "Write-Host" part.
What you can do also is generate a CSV file with those groups:

  #Create an array to store the empty groups
  $AllEmptyGroups = @() 
  #Get all SCOM groups
  $Groups = Get-SCOMGroup 
     
  #Check if the group has members or not
  foreach ($Group in $Groups) {
  if (!($Group.GetRelatedMonitoringObjects())) {
  #Group Display Name
  $CurrentEmptyGroup = $Group.DisplayName
  Write-Host "The group $($Group.DisplayName) is empty" -ForegroundColor Green
  #Add the current empty group to the collection
  $AllEmptyGroups += $CurrentEmptyGroup
  } else {
  Write-Host "The group $($Group.DisplayName)is NOT empty" -ForegroundColor Red
  }
  }
  #Output all empty groups
  $AllEmptyGroups | Export-Csv C:\Temp\EmptyGroups.csv -Delimiter ";" -NoTypeInformation -Encoding UTF8

Hope I could help.


(If the reply was helpful please don't forget to upvote or accept as answer, thank you)
Regards,
Stoyan


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

jg-4093 avatar image
0 Votes"
jg-4093 answered StoyanChalakov commented

Thank you very much for your help and time Stoyan!

The first script is what we need right now so I'll use it for now. I also tried the second one but I only got a bunch of columns with numbers when i opened the csv?

Anyway, thank you for your help again.

Regards
Jakob

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi Jakob,
glad to help you out. If you run the second script and generate a .csv file, please open it with excel and follow this (attention, it is not a Microsoft link):

How to open a CSV file in Excel?
https://www.copytrans.net/support/how-to-open-a-csv-file-in-excel/

and you will get a nice output :)

Hope I was able to help. Regards!
Stoyan

0 Votes 0 ·