Export (0) Print
Expand All
1 out of 2 rated this helpful - Rate this topic

How to Install, Remove, or Repair an Agent

System Center

Updated: May 22, 2009

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

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.
            string serverName = "EnterFullyQualifiedServerNameHere";

            ManagementGroupAdministration admin = mg.GetAdministration();

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

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

            // Get the failover server for the new agent.
            string failoverServerName = "EnterFailoverServerNameHere";
            query = "Name = '" + failoverServerName + "'";

            ManagementServerCriteria failoverCriteria = new ManagementServerCriteria(query);
            ReadOnlyCollection<ManagementServer> failoverServers = 
                admin.GetManagementServers(failoverCriteria);
            if (failoverServers.Count != 1)
                throw new InvalidOperationException("Error! Expected one failover server: " + query);

            // Create a custom monitoring object for the Windows-based computer
            // that will host the agent.
            List<CustomMonitoringObject> agentsTemp = new List<CustomMonitoringObject>();
            MonitoringClass computerType = mg.GetMonitoringClass(SystemMonitoringClass.WindowsComputer);
            MonitoringClassProperty principalNameProperty = computerType.GetMonitoringProperty("PrincipalName");
            MonitoringClassProperty networkNameProperty = computerType.GetMonitoringProperty("NetworkName");
            CustomMonitoringObject entity = new CustomMonitoringObject(computerType);

            // Fully qualified name of the Windows-based computer.
            string fullAgentComputerName = "EnterAgentComputerNameHere";
            entity.SetMonitoringPropertyValue(principalNameProperty, fullAgentComputerName);

            // Short network name (not fully qualified) of the agent computer.
            string agentNetworkName = "EnterAgentNetworkNameHere";
            entity.SetMonitoringPropertyValue(networkNameProperty, agentNetworkName);

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

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

            // Display results.
            bool allSucceeded = true;
            foreach (MonitoringTaskResult result in results.MonitoringTaskResults)
            {
                Console.WriteLine("Status: " + result.Status + " " + result.ErrorMessage);
                if (result.Status != 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";

            ManagementGroupAdministration admin = mg.GetAdministration();

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

            ReadOnlyCollection<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);
            ReadOnlyCollection<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 (MonitoringTaskResult result in results.MonitoringTaskResults)
            {
                Console.WriteLine("Status: " + result.Status + ", " + result.ErrorMessage);
                if (result.Status != 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";

            ManagementGroupAdministration admin = mg.GetAdministration();

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

            ReadOnlyCollection<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);
            ReadOnlyCollection<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 (MonitoringTaskResult result in results.MonitoringTaskResults)
            {
                Console.WriteLine("Status: " + result.Status + ", " + result.ErrorMessage);
                if (result.Status != TaskStatus.Succeeded)
                    allSucceeded = false;
            }

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


        }
    }
}

See Also

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.