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

Tuesday, October 16, 2012

SCOM 2007/2012 Cheat Sheet

What I'm about to cover is not located on anyone elses site.  And if it is, they just ripped it off from here.

Cheat Sheet #1: The poor man's managementpack working environment using SCOM 2007.

Thought you had to have the SCOM Console or SCOM Authoring Console installed and working correctly just to create a ManagementPack? You thought wrong.

Manually install an SCOM 2007 agent on your computer. Copy the SDK binaries into the agent's folder, bring up a copy of Visual Studio, create a project and then add a reference to them by browsing over to the agent's folder and selecting each one of the binaries.  You only need the application one.

Then, add the namespaces to your form or module or class:

VB.Net:
Imports Microsoft.EnterpriseManagement.Configuration
Imports Microsoft.EnterpriseManagement.Configuration.IO

C#.Net:
using Microsoft.EnterpriseManagement.Configuration;
using Microsoft.EnterpriseManagement.Configuration.IO;

C++.Net:
using namespace Microsoft::EnterpriseManagement::Configuration;
using namespace Microsoft::EnterpriseManagement::Configuration::IO;

Cheat Sheet #2: The poor man's managementpack working environment using SCOM 2012.

Manually install an SCOM 2012 agent on your computer. Copy the SDK binaries into the agent's folder, bring up a copy of Visual Studio, create a project and then add a reference to them by browsing over to the agent's folder and selecting each one of the binaries.  You only need the core one.

Then, add the namespaces to your form or module or class:

VB.Net:
Imports Microsoft.EnterpriseManagement.Configuration
Imports Microsoft.EnterpriseManagement.Configuration.IO

C#.Net:
using Microsoft.EnterpriseManagement.Configuration;
using Microsoft.EnterpriseManagement.Configuration.IO;

C++.Net:
using namespace Microsoft::EnterpriseManagement::Configuration;
using namespace Microsoft::EnterpriseManagement::Configuration::IO;

This one is a bit trickier in that you have to change the compiler option from .Net Framework 4 Client Profile to .Net Framework 4. See picture below:




Cheat Sheet #3: Both sealed and unsealed Managementpacks are MPs as far as Managmentpacks are concerned. When you make reference to an mp either format is accepted by the following call:

Dim mp as New ManagementPack(mpFilename)
Dim mp as New ManagementPack(xmlFilename)

Cheat Sheet #4: Covert an unsealed SCOM 2007 mp to an unsealed SCOM 2012 mp in just two lines of code but you have to use the 2012 SDK binaries. Use only a copy of your original SCOM 2007 mp (sealed or unsealed). Place it into a new folder. Below is an example of how to do this using a sealed mp:

Dim OpenFileDialog As New OpenFileDialogOpenFileDialog.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocumentsOpenFileDialog.Filter =
"Sealed Managementpack (*.mp)|*.mp"If (OpenFileDialog.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK) ThenDim FileName As String = OpenFileDialog.FileName
Dim mp As New ManagementPack(FileName)
Dim xmlWriter As New ManagementPackXmlWriter(Application.StartupPath)
Dim xmlFilename As String = xmlWriter.WriteManagementPack(mp)mp =
New mp(xmlFilename)mp.AcceptChanges()
End If
If you try doing the code below, you will get an error:

Dim OpenFileDialog As New OpenFileDialogOpenFileDialog.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocumentsOpenFileDialog.Filter =
"Sealed Managementpack (*.mp)|*.mp"If (OpenFileDialog.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK) ThenDim FileName As String = OpenFileDialog.FileName
Dim mp As New ManagementPack(FileName)mp.AcceptChanges()End If

The image below proves it is trying to write to disk:



Here's an example of doing the same thing using the unsealed xml file:

Dim OpenFileDialog As New OpenFileDialogOpenFileDialog.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocumentsOpenFileDialog.Filter =
"Unsealed Managementpack (*.xml)|*.xml"If (OpenFileDialog.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK) ThenDim FileName As String = OpenFileDialog.FileName
Dim mp As New ManagementPack(FileName)mp.AcceptChanges()End If

Cheat Sheet #5: You can't verify your xml.  You have to use the Authoring Console. Wanna bet?


Dim OpenFileDialog As New OpenFileDialog
OpenFileDialog.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments
OpenFileDialog.Filter = "Unsealed Managementpack (*.xml)|*.xml"
If (OpenFileDialog.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK) Then
Dim FileName As String = OpenFileDialog.FileNameDim mpStore as new ManagementFileStore
'I'm using SCOM 2012 as an example
mpStore.AddDirectory("C:\SC2012OMRTM\ManagementPacks")

Dim mp As New ManagementPack(FileName, mpStore)mp.Verify()End If

NOTE: This assumes you're using the standard out of the box libaray mps

Cheat Sheet #6: You don't have to have hard code every library and other referenced managementpacks.

When dynamically adding librarys and other referenced mamagmentpacks, create an a array of mps instead.
This way, your libraries can be dynamically driven by the design of your ManagementPack.