How to Query for Computers Running Windows Server 2003

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

You can query for computers running Windows Server 2003 or for other instances of a Management Object by first querying for the Management Class that defines the type of the instances. After the type is retrieved, you can then query for the instances by using the GetPartialMonitoringObjects method or the GetMonitoringObjects method. The following example queries for all the instances of computers running Windows Server 2003.

/// <summary> 
/// Query for all computers running Windows Server 2003 in the management group.
/// </summary>
using System;
using System.Collections.ObjectModel;
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
            // computers running Windows Server 2003.
            MonitoringClassCriteria classCriteria = 
                new MonitoringClassCriteria("Name = 'Microsoft.Windows.Server.2003.Computer'");
            
            Console.WriteLine("Querying for data...");
            // There should only be one item in the monitoringClasses collection.
            ReadOnlyCollection<MonitoringClass> monitoringClasses = 
                mg.GetMonitoringClasses(classCriteria);
            
            // Get all instances of computers running Windows Server 2003 in the management group.
            ReadOnlyCollection<PartialMonitoringObject> monitoringObjects =
                mg.GetPartialMonitoringObjects(monitoringClasses[0]);

            // Display information about each computer running Windows Server 2003.
            foreach (PartialMonitoringObject monitoringObject in monitoringObjects)
            {
                Console.WriteLine("Server name: " + monitoringObject.Name);
                Console.WriteLine("Last modified: " + monitoringObject.LastModified +
                    Environment.NewLine);
            }
            
        }
    }
}