How to Install, Remove, or Repair an Agent

Updated: January 31, 2012

Applies To: System Center 2012 - Operations Manager

The Operations Manager class libraries can be used to automate the deployment of agents to Windows-based computers. This allows you to easily use databases and other custom sources of deployment data to control the installation and configuration of agents.

Example

Example 1—Installing an Agent

The following example demonstrates how to configure installation settings and deploy an agent to a Windows-based computer:

/// <summary>
/// Deploys an agent to the specified Windows-based computer.
/// </summary>
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Microsoft.EnterpriseManagement;
using Microsoft.EnterpriseManagement.Administration;
using Microsoft.EnterpriseManagement.Common;
using Microsoft.EnterpriseManagement.Configuration;
using Microsoft.EnterpriseManagement.Monitoring;
using System.Text;

namespace SDKSamples
{
	class Program
	{
		static void Main(string[] args)
		{
			ManagementGroup mg = new ManagementGroup("localhost");

			Console.WriteLine("Starting to deploy and install an agent...");

			// Define the Management Server that will manage the new agent.
			IAdministrationManagement admin = mg.Administration;

			IList<ManagementServer> servers = admin.GetManagementServers(new ManagementServerCriteria("Name = 'EnterFullyQualifiedServerNameHere'"));
			if (servers.Count != 1)
				throw new InvalidOperationException("Error! Expected one Management Server: EnterFullyQualifiedServerNameHere");

			// Get the failover server for the new agent.
			ManagementServerCriteria failoverCriteria = new ManagementServerCriteria("Name = 'EnterFailoverServerNameHere'");
			IList<ManagementServer> failoverServers = 
				admin.GetManagementServers(failoverCriteria);
			if (failoverServers.Count != 1)
				throw new InvalidOperationException("Error! Expected one failover server: EnterFailoverServerNameHere");

			// Create a custom monitoring object for the Windows-based computer
			// that will host the agent.
			List<CreatableEnterpriseManagementObject> agentsTemp = new List<CreatableEnterpriseManagementObject>();
			ManagementPackClass computerType = mg.EntityTypes.GetClass(SystemMonitoringClass.WindowsComputer);
			ManagementPackProperty principalNameProperty = computerType["PrincipalName"];
			ManagementPackProperty networkNameProperty = computerType["NetworkName"];

			CreatableEnterpriseManagementObject entity = new CreatableEnterpriseManagementObject(mg, computerType);

			// Fully qualified name of the Windows-based computer.
			entity[principalNameProperty].Value = "EnterAgentComputerNameHere";

			// Short network name (not fully qualified) of the agent computer.
			entity[networkNameProperty].Value ="EnterAgentNetworkNameHere";

			agentsTemp.Add(entity);
			ReadOnlyCollection<CreatableEnterpriseManagementObject> agents =
				new ReadOnlyCollection<CreatableEnterpriseManagementObject>(agentsTemp);

			// Install the agent.
			InstallAgentConfiguration configuration = new InstallAgentConfiguration();
			AgentTaskResult results = servers[0].InstallAgents(agents, failoverServers, configuration);

			// Display results.
			bool allSucceeded = true;
			foreach (Microsoft.EnterpriseManagement.Runtime.TaskResult result in results.TaskResults)
			{
				Console.WriteLine("Status: " + result.Status + " " + result.ErrorMessage);
				if (result.Status != Microsoft.EnterpriseManagement.Runtime.TaskStatus.Succeeded)
					allSucceeded = false;
		}

			if (allSucceeded)
				Console.WriteLine("DeployAndInstallAgent succeeded.");
			else
				Console.WriteLine("Not all agent installations succeeded.");
		 
	}
}
}

Example 2—Removing an Agent

The following example demonstrates how to remove an agent from an agent-managed computer:

/// <summary>
/// Removes an agent from the specified agent-managed computer.
/// </summary>
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Microsoft.EnterpriseManagement;
using Microsoft.EnterpriseManagement.Administration;
using Microsoft.EnterpriseManagement.Common;
using Microsoft.EnterpriseManagement.Configuration;
using Microsoft.EnterpriseManagement.Monitoring;
using System.Text;

namespace SDKSamples
{
	class Program
	{
		static void Main(string[] args)
		{
			ManagementGroup mg = new ManagementGroup("localhost");

			Console.WriteLine("Starting to uninstall an agent...");

			// Define the Management Server that manages the agent.
			string serverName = "EnterFullyQualifiedServerNameHere";

			IAdministrationManagement admin = mg.Administration;

			string query = "Name = '" + serverName + "'";
			ManagementServerCriteria serverCriteria = new ManagementServerCriteria(query);

			IList<ManagementServer> servers = admin.GetManagementServers(serverCriteria);
			if (servers.Count != 1)
				throw new InvalidOperationException("Error! Expected one Management Server: " + query);

			// Fully qualified name of the agent-managed computer.
			string fullAgentComputerName = "EnterAgentComputerNameHere";
			query = "Name = '" + fullAgentComputerName + "'";
			AgentManagedComputerCriteria agentCriteria =
				new AgentManagedComputerCriteria(query);
			IList<AgentManagedComputer> agents =
				admin.GetAgentManagedComputers(agentCriteria);
			if (agents.Count != 1)
				throw new InvalidOperationException("Error! Expected one managed computer with: " + query);

			// Uninstall the agent.
			UninstallAgentConfiguration configuration = new UninstallAgentConfiguration();
			AgentTaskResult results = servers[0].UninstallAgents(agents, configuration);

			bool allSucceeded = true;
			foreach (Microsoft.EnterpriseManagement.Runtime.TaskResult result in results.TaskResults)
			{
				Console.WriteLine("Status: " + result.Status + ", " + result.ErrorMessage);
				if (result.Status != Microsoft.EnterpriseManagement.Runtime.TaskStatus.Succeeded)
					allSucceeded = false;
		}

			if (allSucceeded)
				Console.WriteLine("Uninstalled Agents successfully.");
			else
				Console.WriteLine("Not all agents were uninstalled.");

	}
}
}

Example 3—Repairing an Agent

The following example demonstrates how to repair an agent installation on an agent-managed computer:

/// <summary>
/// Repairs the agent on the specified agent-managed computer.
/// </summary>
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Microsoft.EnterpriseManagement;
using Microsoft.EnterpriseManagement.Administration;
using Microsoft.EnterpriseManagement.Common;
using Microsoft.EnterpriseManagement.Configuration;
using Microsoft.EnterpriseManagement.Monitoring;
using System.Text;

namespace SDKSamples
{
	class Program
	{
		static void Main(string[] args)
		{
			ManagementGroup mg = new ManagementGroup("localhost");

			Console.WriteLine("Starting to repair an agent...");

			// Define the Management Server that manages the agent.
			string serverName = "EnterFullyQualifiedServerNameHere";

			IAdministrationManagement admin = mg.Administration;

			string query = "Name = '" + serverName + "'";
			ManagementServerCriteria serverCriteria = new ManagementServerCriteria(query);

			IList<ManagementServer> servers = admin.GetManagementServers(serverCriteria);
			if (servers.Count != 1)
				throw new InvalidOperationException("Error! Expected one Management Server: " + query);

			// Fully qualified name of the agent-managed computer.
			string fullAgentComputerName = "EnterAgentComputerNameHere";
			query = "Name = '" + fullAgentComputerName + "'";
			AgentManagedComputerCriteria agentCriteria =
				new AgentManagedComputerCriteria(query);
			IList<AgentManagedComputer> agents =
				admin.GetAgentManagedComputers(agentCriteria);
			if (agents.Count != 1)
				throw new InvalidOperationException("Error! Expected one managed computer with: " + query);

			RepairAgentConfiguration configuration = new RepairAgentConfiguration();

			// Repair the agent.
			AgentTaskResult results = servers[0].RepairAgents(agents, configuration);

			bool allSucceeded = true;
			foreach (Microsoft.EnterpriseManagement.Runtime.TaskResult result in results.TaskResults)
			{
				Console.WriteLine("Status: " + result.Status + ", " + result.ErrorMessage);
				if (result.Status != Microsoft.EnterpriseManagement.Runtime.TaskStatus.Succeeded)
					allSucceeded = false;
		}

			if (allSucceeded)
				Console.WriteLine("RepairAgent succeeded.");
			else
				Console.WriteLine("Not all agents were repaired.");


	}
}
}

See Also