How to Delete a Group

System Center

Updated: May 22, 2009

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.

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")

                DeleteGroup(mg, "SampleGroup1")
            Catch [error] As MonitoringException
            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")
                Dim monitoringClasses As ReadOnlyCollection(Of MonitoringClass)
                monitoringClasses = groupToDelete.GetMonitoringClasses()
            End If

        End Sub 'DeleteGroup

    End Class 'Program
End Namespace 'SDKSamples