Udostępnij za pośrednictwem


How to Delete Views and Folders

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

You can delete views and folders from a Management Pack that is not sealed. The following example demonstrates how to delete a view and a folder that were previously created.

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

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

            try
            {
                DeleteView(mg, "SampleView1");
                DeleteFolder(mg, "SampleFolder");
            }
            catch (MonitoringException error)
            {
                Console.WriteLine(error.Message);
            }
        }

        //---------------------------------------------------------------------
        private static void DeleteFolder(
            ManagementGroup mg,
            string folderDisplayName
            )
        {
            ReadOnlyCollection<MonitoringFolder> folders;
            MonitoringFolderCriteria folderCriteria;
            MonitoringFolder folder;

            folderCriteria = new MonitoringFolderCriteria(
                string.Format("DisplayName='{0}'", folderDisplayName));
            folders = mg.GetMonitoringFolders(folderCriteria);

            if (folders.Count != 1)
            {
                throw new ApplicationException("Failed to retrieve the specified folder");
            }

            folder = folders[0];

            if (folder.GetManagementPack().Sealed)
            {
                throw new ApplicationException(
                    "Its not possible to delete folders in a sealed management pack");
            }

            // Mark the folder for deletion.
            folder.Status = ManagementPackElementStatus.PendingDelete;

            // Commit the changes.
            folder.GetManagementPack().AcceptChanges();
        }

        //---------------------------------------------------------------------
        private static void DeleteView(
            ManagementGroup mg,
            string viewDisplayName
            )
        {
            ReadOnlyCollection<MonitoringView> views;
            MonitoringViewCriteria viewCriteria;
            MonitoringView view;

            viewCriteria = new MonitoringViewCriteria(
                string.Format("DisplayName='{0}'", viewDisplayName));
            views = mg.GetMonitoringViews(viewCriteria);

            if (views.Count != 1)
            {
                throw new ApplicationException("Failed to retrieve the specified view");
            }

            view = views[0];

            if (view.GetManagementPack().Sealed)
            {
                throw new ApplicationException(
                    "Its not possible to delete views in a sealed management pack");
            }

            // Mark the view for deletion.
            view.Status = ManagementPackElementStatus.PendingDelete;

            // Commit the changes.
            view.GetManagementPack().AcceptChanges();
        }
    }
}
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

                DeleteView(mg, "SampleView1")
                DeleteFolder(mg, "SampleFolder")

            Catch err As MonitoringException

                Console.WriteLine(err.Message)
            End Try
        End Function

        '---------------------------------------------------------------------
        Private Shared Sub DeleteFolder( _
            ByVal mg As ManagementGroup, _
            ByVal folderDisplayName As String)

            Dim folders As ReadOnlyCollection(Of MonitoringFolder)
            Dim folderCriteria As MonitoringFolderCriteria
            Dim folder As MonitoringFolder

            folderCriteria = New MonitoringFolderCriteria( _
                String.Format("DisplayName='{0}'", folderDisplayName))
            folders = mg.GetMonitoringFolders(folderCriteria)

            If (folders.Count <> 1) Then
                Throw New ApplicationException("Failed to retrieve the specified folder")
            End If

            folder = folders(0)

            If (folder.GetManagementPack().Sealed) Then

                Throw New ApplicationException( _
                    "Its not possible to delete folders in a sealed management pack")
            End If

            ' Mark the folder for deletion.
            folder.Status = ManagementPackElementStatus.PendingDelete

            ' Commit the changes.
            folder.GetManagementPack().AcceptChanges()
        End Sub

        '---------------------------------------------------------------------
        Private Shared Sub DeleteView( _
            ByVal mg As ManagementGroup, _
            ByVal viewDisplayName As String)

            Dim views As ReadOnlyCollection(Of MonitoringView)
            Dim viewCriteria As MonitoringViewCriteria
            Dim view As MonitoringView

            viewCriteria = New MonitoringViewCriteria( _
                String.Format("DisplayName='{0}'", viewDisplayName))
            views = mg.GetMonitoringViews(viewCriteria)

            If (views.Count <> 1) Then

                Throw New ApplicationException("Failed to retrieve the specified view")
            End If

            view = views(0)

            If (view.GetManagementPack().Sealed) Then

                Throw New ApplicationException( _
                    "Its not possible to delete views in a sealed management pack")
            End If

            ' Mark the view for deletion.
            view.Status = ManagementPackElementStatus.PendingDelete

            ' Commit the changes.
            view.GetManagementPack().AcceptChanges()
        End Sub
    End Class
End Namespace