How to Delete a Group

Applies To: Operations Manager 2007 R2, Operations Manager 2007 SP1, System Center Operations Manager 2007

You must know the name of the group you want to delete, and then you can delete the group from the Management Pack that contains the group. The following code example deletes a group named SampleGroup1. The DeleteMonitoringObjectGroup method is used to delete the group.

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Microsoft.EnterpriseManagement;
using Microsoft.EnterpriseManagement.Configuration;
using Microsoft.EnterpriseManagement.Common;
using Microsoft.EnterpriseManagement.Monitoring;

namespace SDKSamples
{
    class Program
    {
        static void Main(string[] args)
        {
            ManagementGroup mg;

            mg = new ManagementGroup("localhost");

            try
            {
                DeleteGroup(mg, "SampleGroup1");
            }
            catch (MonitoringException error)
            {
                Console.WriteLine(error.Message);
            }
        }

        // ---------------------------------------------------------------------
        private static void DeleteGroup(
            ManagementGroup mg,
            string groupDisplayName
            )
        {
            ReadOnlyCollection<MonitoringObjectGroup> groups;
            MonitoringObjectGroup groupToDelete = null;

            groups = mg.GetRootMonitoringObjectGroups();

            foreach (MonitoringObjectGroup group in groups)
            {
                if (group.DisplayName == groupDisplayName)
                {
                    groupToDelete = group;
                    break;
                }
            }

            if (groupToDelete == null)
            {
                throw new ApplicationException("Failed to retrieve the specified group");
            }
            else
            {
                ReadOnlyCollection<MonitoringClass> monitoringClasses;
                monitoringClasses = groupToDelete.GetMonitoringClasses();
                monitoringClasses[0].GetManagementPack().DeleteMonitoringObjectGroup(groupToDelete);
            }
        }
    }
}
Imports System
Imports System.Collections.Generic
Imports System.Collections.ObjectModel
Imports Microsoft.EnterpriseManagement
Imports Microsoft.EnterpriseManagement.Configuration
Imports Microsoft.EnterpriseManagement.Common
Imports Microsoft.EnterpriseManagement.Monitoring


Namespace SDKSamples
    Class Program
        Public Overloads Shared Function Main(ByVal args() As String) As Integer

            Dim mg As ManagementGroup
            mg = New ManagementGroup("localhost")

            Try
                DeleteGroup(mg, "SampleGroup1")
            Catch [error] As MonitoringException
                Console.WriteLine([error].Message)
            End Try
        End Function 'Main


        ' ---------------------------------------------------------------------
        Private Shared Sub DeleteGroup(ByVal mg As ManagementGroup, _
            ByVal groupDisplayName As String)

            Dim groups As ReadOnlyCollection(Of MonitoringObjectGroup)
            Dim groupToDelete As MonitoringObjectGroup = Nothing

            groups = mg.GetRootMonitoringObjectGroups()

            For Each group As MonitoringObjectGroup In groups
                If group.DisplayName = groupDisplayName Then
                    groupToDelete = group
                    Exit For
                End If
            Next group

            If groupToDelete Is Nothing Then
                Throw New ApplicationException("Failed to retrieve the specified group")
            Else
                Dim monitoringClasses As ReadOnlyCollection(Of MonitoringClass)
                monitoringClasses = groupToDelete.GetMonitoringClasses()
                monitoringClasses(0).GetManagementPack().DeleteMonitoringObjectGroup(groupToDelete)
            End If

        End Sub 'DeleteGroup

    End Class 'Program
End Namespace 'SDKSamples