SCOM 2012 Hello World !

I’ve been investigating how to connect to SCOM 2012 through code in order to extract information, and I thought a good first task (therefore a SCOM 2012 Hello World if you will ;)) would be to retrieve the current alerts. I couldn’t find any examples on how to do this, so after some searching came up with the following.

I have used some sample code from Stackoverflow to read in the password for the user and have attributed the code with the source URL below.

The full code is as follows:

using System;
using System.Collections.Generic;
using System.Security;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Microsoft.EnterpriseManagement;
using Microsoft.EnterpriseManagement.Administration;
using Microsoft.EnterpriseManagement.Common;
using Microsoft.EnterpriseManagement.Configuration;
using Microsoft.EnterpriseManagement.Monitoring;

namespace SCOMHelloWorld
{
    class Program
    {
        static string scom_management_server = "scom.fqdn";
        static string scom_domain = "DOMAIN";
        static string scom_username = "Administrator";

        static void Main(string[] args)
        {
            Console.WriteLine("Enter the password for the '" + scom_username + "@" + scom_domain + "' user account:");
            string password = ReadPassword();

            ManagementGroupConnectionSettings settings = new ManagementGroupConnectionSettings(scom_management_server);
            settings.Domain = scom_domain;
            settings.UserName = scom_username;
            settings.Password = new SecureString();
            Array.ForEach(password.ToArray(), settings.Password.AppendChar);
            settings.Password.MakeReadOnly();

            try
            {
                ManagementGroup mg = new ManagementGroup(settings);
                IList alerts = mg.OperationalData.GetMonitoringAlerts(MonitoringAlertCriteria.Empty, default(DateTime));

                if (alerts.Count > 0)
                {
                    foreach (MonitoringAlert alert in alerts)
                    {
                        Console.WriteLine();
                        Console.WriteLine("Name: " + alert.Name);
                        Console.WriteLine("Resolution state: " + alert.ResolutionState.ToString());
                    }
                }
                else
                {
                    Console.WriteLine("No alerts found.");
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception " + e.GetType().FullName + " encountered: " + e.Message);
            }

            Console.WriteLine("Press any key to continue.");
            Console.ReadKey();
        }

        // From: http://stackoverflow.com/questions/29201697/hide-replace-when-typing-a-password-c
        static string ReadPassword()
        {
            string password = "";
            ConsoleKeyInfo info = Console.ReadKey(true);
            while (info.Key != ConsoleKey.Enter)
            {
                if (info.Key != ConsoleKey.Backspace)
                {
                    Console.Write("*");
                    password += info.KeyChar;
                }
                else if (info.Key == ConsoleKey.Backspace)
                {
                    if (!string.IsNullOrEmpty(password))
                    {
                        // remove one character from the list of password characters
                        password = password.Substring(0, password.Length - 1);
                        // get the location of the cursor
                        int pos = Console.CursorLeft;
                        // move the cursor to the left by one character
                        Console.SetCursorPosition(pos - 1, Console.CursorTop);
                        // replace it with space
                        Console.Write(" ");
                        // move the cursor to the left by one character again
                        Console.SetCursorPosition(pos - 1, Console.CursorTop);
                    }
                }
                info = Console.ReadKey(true);
            }
            // add a new line because user pressed enter at the end of their password
            Console.WriteLine();
            return password;
        }
    }
}

Note that if the user’s password has expired and needs to be changed, the SCOM Management Server will reject the connection with an exception, so if you get an error remember to check the domain account manually.

The default resolution state values that are supplied with SCOM are below, though obviously custom ones can be defined in the Administration space.

This project will be uploaded to github shortly (and the location noted).