Compartilhar via


How to Query for Agents

Applies To: Operations Manager 2007 R2, Operations Manager 2007 SP1, System Center Operations Manager 2007

An agent is an Operations Manager service that runs on each computer that you want to monitor. An agent captures information from the computer on which it is running, applies predefined rules to the captured data, and performs actions as defined by the rules. You can query for agents by defining criteria in the AgentManagedComputerCriteria 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

  • LastModified

  • DisplayName

The following code queries for all the agents that were modified after 10/25/2006.

/// <summary> 
/// Query for agents.
/// </summary>
using System;
using System.Collections.ObjectModel;
using Microsoft.EnterpriseManagement;
using Microsoft.EnterpriseManagement.Administration;
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
            // agents that were modified after 10/25/2006.
            AgentManagedComputerCriteria agentCriteria =
                new AgentManagedComputerCriteria(
                "LastModified >= '" +
                new DateTime(2006, 10, 25).ToString("G") + "'");

            ManagementGroupAdministration administration =
                mg.GetAdministration();

            Console.WriteLine("Querying for data...");
            ReadOnlyCollection<AgentManagedComputer> agents =
                administration.GetAgentManagedComputers(agentCriteria);

            // Display information about each agent.
            foreach (AgentManagedComputer agent in agents)
            {
                Console.WriteLine("Agent name: " + agent.Name);
                Console.WriteLine("IPAddress: " + agent.IPAddress);
                Console.WriteLine("Health State: " + agent.HealthState);
                Console.WriteLine("Version: " + agent.Version +
                    Environment.NewLine);
            }
        }
    }
}