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:
/// A class to be used in conjunction with the Manual Entity State Helper management pack to quickly set the state of any instance.
public class EntityStateHelper
/// Sets the state of an instance.
/// <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)
// Set the proper event id based on the state
eventNumber = 1;
eventNumber = 2;
eventNumber = 3;
// Insert the event
CustomMonitoringEvent stateEvent = new CustomMonitoringEvent("EntityStateHelper", eventNumber);
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
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.