How to Query for Computers Running Windows Server 2003

Updated: January 31, 2012

Applies To: System Center 2012 - Operations Manager

You can query for computers running Windows Server 2003 or for other instances of a monitoring object by first querying for the management pack class that defines the type of the instances. After the type is retrieved, you can then query for the instances by using the 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.Generic;
using Microsoft.EnterpriseManagement;
using Microsoft.EnterpriseManagement.Common;
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.
			ManagementPackClassCriteria classCriteria =
				new ManagementPackClassCriteria("Name = 'Microsoft.Windows.Server.2003.Computer'");
		
			Console.WriteLine("Querying for data...");
			// There should only be one item in the monitoringClasses collection.
			IList<ManagementPackClass> monitoringClasses = mg.EntityTypes.GetClasses(classCriteria);
		
			// Get all instances of computers running Windows Server 2003 in the management group.
			List<PartialMonitoringObject> monitoringObjects = new List<PartialMonitoringObject>();
			IObjectReader<PartialMonitoringObject> reader = mg.EntityObjects.GetObjectReader<PartialMonitoringObject>(monitoringClasses[0], ObjectQueryOptions.Default);
			monitoringObjects.AddRange(reader);

			// 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);
		}
		
	}
}
}

See Also