How to Query for Tasks

Updated: January 31, 2012

Applies To: System Center 2012 - Operations Manager

In Operations Manager, you can run predefined tasks that are included in your imported management packs, or you can create your own tasks. You can have a batch file or script run as a task remotely or locally. You can query for tasks by defining criteria in the class constructor. The criteria syntax is defined in Criteria Expression Syntax. The following property names are valid names that can be used in the criteria expression:

  • Id

  • Name

  • ManagementPackId

  • Accessibility

  • Enabled

  • Application

  • WorkingDirectory

  • Parameters

  • DisplayName

  • Description

  • Target

  • TimeAdded

  • LastModified

  • RequireOutput

  • LastModified

  • Category

  • CategoryOld

  • Assembly

  • HandlerAssembly

The following code queries for the maintenance tasks that have SystemCenter in their name:

/// <summary> 
/// Query for tasks.
/// </summary>
using System;
using System.Collections.Generic;
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 maintenance tasks that have SystemCenter in their name.
			ManagementPackTaskCriteria taskCriteria =
				new ManagementPackTaskCriteria(
				"Category = 'Maintenance' AND Name LIKE '%SystemCenter%'");

			Console.WriteLine("Querying for data...");
			IList<ManagementPackTask> tasks =
				mg.TaskConfiguration.GetTasks(taskCriteria);

			// Display information about each task.
			foreach (ManagementPackTask task in tasks)
			{
				Console.WriteLine("Task name: " + task.Name);
				Console.WriteLine("Status: " + task.Status.ToString());
				Console.WriteLine("Category: " + task.Category);
				Console.WriteLine("Description: " + task.Description +
					Environment.NewLine);
		}
	}
}
}

See Also