Share via


Submitting Tasks with Additional Configuration

I had a post earlier about submitting tasks, but I did not discuss how to specify additional configuration when submitting these tasks at that time. The two things that are configurable for tasks are credentials and overrides. Credentials allow you to change what user the task is executed as, while overrides allow you to change some of the configuration parameters that are defined as overrideable for the task.

To determine what is overrideable on a particular task, you have to look at the task definition to find the module type the task in implementing. In the attached sample management pack, the module type used by the task, System.Health.ResetState, is System.Health.SetTargetStateAction. When you look at that module type in the sample management pack, you will notice it has a single overrideable parameter called MonitorId which is defined as a string.

In order to provide the override to the task, you must specify, per overrideable parameter, the new value you would like to use within the MonitoringTaskConfiguration object. If the module type of the task has no overrideable parameters, you can't override anything. The below sample along with the attached management pack shows how to do this in the SDK.

Note: I pulled this sample out of one of our existing management packs that are internal to SCOM. This task can be executed indirectly by calling the various ResetMonitoringState methods in the SDK. Also note that the sample will not actually "work" in the sense that the credentials are bogus and the monitor id I am passing in is also bogus, but it should illustrate how to do this process legitimately for other tasks.

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

namespace Jakub_WorkSamples
{
    partial class Program
    {
        static void RunningTasksWithConfiguration()
        {
            // Connect to the local management group
            ManagementGroup mg = new ManagementGroup("localhost");

            // First lets get the task we are going to execute
            // Note: Indexing the result like this is not necessarily the best 
            // practice here, since there could be more than one task with this
            // name.
            MonitoringTaskCriteria taskCriteria = new MonitoringTaskCriteria(
                "Name = 'System.Health.ResetState'");
            MonitoringTask resetStateTask =
                mg.GetMonitoringTasks(taskCriteria)[0];

            // Configuration allows you to specify credentials to use different
            // from the default. It also allows you to specify overrides, if
            // any are available
            MonitoringTaskConfiguration resetStateTaskConfiguration =
                new MonitoringTaskConfiguration();

            // Specifying credentials
            SecureString password = new SecureString();
            password.AppendChar('p');
            password.AppendChar('a');
            password.AppendChar('s');
            password.AppendChar('s');
            password.AppendChar('w');
            password.AppendChar('o');
            password.AppendChar('r');
            password.AppendChar('d');
            resetStateTaskConfiguration.Credentials =
                new WindowsMonitoringTaskCredentials("Domain", "UserName", password);

            // Specifying the override
            // First we need to get the overrideable parameter that is defined
            //  on the module type that the task uses.
            ManagementPackModuleType setTargetStateWriteAction =
                mg.GetMonitoringModuleTypes("System.Health.SetTargetStateAction")[0];

            // This allows us to override the monitor id
            ManagementPackOverrideableParameter overrideParam =
                setTargetStateWriteAction.OverrideableParameterCollection[0];

            // Set the value of the parameter as a string. In this case it's a Guid.
            resetStateTaskConfiguration.Overrides.Add(
                new Pair<ManagementPackOverrideableParameter, string>(overrideParam,
                    Guid.NewGuid().ToString("B")));

            // Now we need to get an instance to execute this task on. Since this task
            // is targeted to entity, any instance will do.
            MonitoringClass healthServiceClass =
                mg.GetMonitoringClass(SystemMonitoringClass.HealthService);

            // Next get the object (we'll just pick the first one)
            MonitoringObject healthServiceObject =
                mg.GetMonitoringObjects(healthServiceClass)[0];

            // Now we execute the task.
            ReadOnlyCollection<MonitoringTaskResult> results =
                healthServiceObject.ExecuteMonitoringTask(resetStateTask,
                resetStateTaskConfiguration);
        }
    }
}

Task.Sample.xml