Setting the State of an Instance Programmatically

There has been a lot of interest in a simple way to set the state of an instance. Since we don't allow this to happen directly via the SDK, I've had many requests for a simple way to do this so here it is. One caveat: if the instance's state is Error, for instance, from a different monitor, this will not override that state. The monitor this implementation rolls up to has a "Worst Of" algorithm, so even though this monitor as set with the code below is Healthy, there may be another monitor that contributes state that is not. Hope that makes sense.

First, import the attached management pack, Manual.EntityState.Helper.xml. I've left it as unsealed so you can make changes to it if you want. Next you will need this class:

 using System;
using System.Collections.ObjectModel;
using System.Text;
using System.Xml;
using Microsoft.EnterpriseManagement;
using Microsoft.EnterpriseManagement.Monitoring;
using Microsoft.EnterpriseManagement.Configuration;

namespace Jakub_WorkSamples
{
    /// <summary>
    /// A class to be used in conjunction with the Manual Entity State Helper management pack to quickly set the state of any instance.
    /// </summary>
    public class EntityStateHelper
    {
        /// <summary>
        /// Sets the state of an instance.
        /// </summary>
        /// <param name="monitoringObject">The object to set the state of.</param>
        /// <param name="healthState">The state to set to.</param>
        public static void SetState(MonitoringObject monitoringObject, HealthState healthState)
        {
            int eventNumber;

            // Set the proper event id based on the state
            switch (healthState)
            {
                case HealthState.Error:
                    eventNumber = 1;
                    break;
                case HealthState.Warning:
                    eventNumber = 2;
                    break;
                case HealthState.Success:
                    eventNumber = 3;
                    break;
                default:
                    return;
            }

            // Insert the event
            CustomMonitoringEvent stateEvent = new CustomMonitoringEvent("EntityStateHelper", eventNumber);
            monitoringObject.InsertCustomMonitoringEvent(stateEvent);
        }
    }
}

Usage is pretty simple:

 // Connect to the local management group
ManagementGroup mg = new ManagementGroup("localhost");

// Get the agent class
MonitoringClass hs = mg.GetMonitoringClass(SystemMonitoringClass.HealthService);

// Get an agent
ReadOnlyCollection<MonitoringObject> monitoringObjects = mg.GetMonitoringObjects(hs);

// Set its state
EntityStateHelper.SetState(monitoringObjects[0], HealthState.Warning);

I have tested this a bit, but there may be places where this doesn't work as this was meant to work for instances hosted by the Root Management Server. If you do run into issues with a scenario, please let me know so I can try to get it figured out.

Update: You cannot use the method described above to set the state of any instance not hosted on the Root Management Server.

Manual.EntityState.Helper.xml