Tuesday, October 23, 2012

SCOM SDK Changes in syntax from 2007 to 2012

If the code you've written -- or if you are simply cutting and pasting the SCOM 2007 SDK examples -- depends on the SCOM 2007 SDK, you may have to rewrite it to get it to work in SCOM 2012.

Below, is an example of 2007 SDK Code:

/// <summary>
/// Query for diagnostics.
/// </summary>
using System;
using System.Collections.ObjectModel;
using Microsoft.EnterpriseManagement;
using Microsoft.EnterpriseManagement.Configuration;
using Microsoft.EnterpriseManagement.Monitoring;

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

            // The criteria specifies that you want to collect
            // all the diagnostics that contain SystemCenter in their name.
            MonitoringDiagnosticCriteria diagnosticCriteria =
                new MonitoringDiagnosticCriteria(
                "Name LIKE '%SystemCenter%'");

            Console.WriteLine("Querying for data...");
            ReadOnlyCollection<MonitoringDiagnostic> diagnostics =
                mg.GetMonitoringDiagnostics(diagnosticCriteria);

            // Display information about each diagnostic.
            foreach (MonitoringDiagnostic diagnostic in diagnostics)
            {
                Console.WriteLine("Diagnostic name: " + diagnostic.Name);
                Console.WriteLine("Status: " + diagnostic.Status.ToString());
                Console.WriteLine("Category: " + diagnostic.Category);
                Console.WriteLine("Description: " + diagnostic.Description +
                    Environment.NewLine);
            }
        }
    }
}


Compare that to what will work in SCOM 2012:

/// <summary>
/// Query for diagnostics.
/// </summary>
using System;
using System.Collections.ObjectModel;
using Microsoft.EnterpriseManagement;
using Microsoft.EnterpriseManagement.Configuration;
using Microsoft.EnterpriseManagement.Monitoring;

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

            // The criteria specifies that you want to collect
            // all the diagnostics that contain SystemCenter in their name.
            ManagementPackDiagnosticCriteria diagnosticCriteria =
                new ManagementPackDiagnosticCriteria(
                "Name LIKE '%SystemCenter%'");

            Console.WriteLine("Querying for data...");
            ReadOnlyCollection<ManagementPackDiagnostic> diagnostics =
                mg.Monitoring.GetMonitoringDiagnostics(diagnosticCriteria);

            // Display information about each diagnostic.
            foreach (ManagementPackDiagnostic diagnostic in diagnostics)
            {
                Console.WriteLine("Diagnostic name: " + diagnostic.Name);
                Console.WriteLine("Status: " + diagnostic.Status.ToString());
                Console.WriteLine("Category: " + diagnostic.Category);
                Console.WriteLine("Description: " + diagnostic.Description +
                    Environment.NewLine);
            }
        }
    }
}


In VB.Net, the new looks like this:

Imports System
Imports System.Collections.ObjectModel
Imports Microsoft.EnterpriseManagement
Imports Microsoft.EnterpriseManagement.Configuration
Imports Microsoft.EnterpriseManagement.Monitoring


Module VbCode


        Public Sub List_Diagnostics()
      
            Dim mg As EnterpriseManagementGroup  = new
            EnterpriseManagementGroup("localhost")

            Dim Criteria As ManagementPackDiagnosticCriteria = new
            ManagementPackDiagnosticCriteria("Name LIKE '%SystemCenter%'")
            Console.WriteLine("Querying for data...")
            
// Display information about each diagnostic.
            for each diagnostic as ManagementPackDiagnostic in

            mg.Monitoring.GetMonitoringDiagnostics(Criteria)
          
                Console.WriteLine("Diagnostic name: " & diagnostic.Name)
                Console.WriteLine("Status: " & diagnostic.Status.ToString())
                Console.WriteLine("Category: " & diagnostic.Category)
                Console.WriteLine("Description: " & diagnostic.Description
                Console.WriteLine(Environment.NewLine)


            Next
       

        End Sub

End Module

No comments:

Post a Comment