Export (0) Print
Expand All

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.

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);
            }
        }
    }
}

Show:
© 2014 Microsoft