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.

' Deploys an agent to the specified Windows-based computer.
Imports System
Imports System.Collections.Generic
Imports System.Collections.ObjectModel
Imports Microsoft.EnterpriseManagement
Imports Microsoft.EnterpriseManagement.Administration
Imports Microsoft.EnterpriseManagement.Configuration
Imports Microsoft.EnterpriseManagement.Common
Imports Microsoft.EnterpriseManagement.Monitoring


Namespace SDKSamples
    Class Program
        Public Overloads Shared Function Main(ByVal args() As String) As Integer

            Dim mg As ManagementGroup = New ManagementGroup("localhost")

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

            ' Define the Management Server that will manage the new agent.
            Dim serverName As String = "EnterFullyQualifiedServerNameHere"

            Dim admin As ManagementGroupAdministration = mg.GetAdministration()

            Dim query As String = "Name = '" & serverName & "'"
            Dim serverCriteria As ManagementServerCriteria = New ManagementServerCriteria(query)

            Dim servers As ReadOnlyCollection(Of ManagementServer) = admin.GetManagementServers(serverCriteria)
            If (servers.Count <> 1) Then
                Throw New InvalidOperationException("Error! Expected one Management Server: " & query)
            End If

            ' Get the failover server for the new agent.
            Dim failoverServerName As String = "EnterFailoverServerNameHere"
            query = "Name = '" & failoverServerName & "'"

            Dim failoverCriteria As ManagementServerCriteria = New ManagementServerCriteria(query)
            Dim failoverServers As ReadOnlyCollection(Of ManagementServer) = _
                admin.GetManagementServers(failoverCriteria)
            If (failoverServers.Count <> 1) Then
                Throw New InvalidOperationException("Error! Expected one failover server: " & query)
            End If

            ' Create a custom monitoring object for the Windows-based computer
            ' that will host the agent.
            Dim agentsTemp As List(Of CustomMonitoringObject) = New List(Of CustomMonitoringObject)()
            Dim computerType As MonitoringClass = mg.GetMonitoringClass(SystemMonitoringClass.WindowsComputer)
            Dim principalNameProperty As MonitoringClassProperty = _
                computerType.GetMonitoringProperty("PrincipalName")
            Dim networkNameProperty As MonitoringClassProperty = computerType.GetMonitoringProperty("NetworkName")
            Dim entity As CustomMonitoringObject = New CustomMonitoringObject(computerType)

            ' Fully qualified name of the Windows-based computer.
            Dim fullAgentComputerName As String = "EnterAgentComputerNameHere"
            entity.SetMonitoringPropertyValue(principalNameProperty, fullAgentComputerName)

            ' Short network name (not fully qualified) of the agent computer.
            Dim agentNetworkName As String = "EnterAgentNetworkNameHere"
            entity.SetMonitoringPropertyValue(networkNameProperty, agentNetworkName)

            agentsTemp.Add(entity)
            Dim agents As ReadOnlyCollection(Of CustomMonitoringObject) = _
                New ReadOnlyCollection(Of CustomMonitoringObject)(agentsTemp)

            ' Install the agent.
            Dim configuration As InstallAgentConfiguration = New InstallAgentConfiguration()
            Dim results As AgentTaskResult = servers(0).InstallAgents(agents, failoverServers, configuration)

            ' Display results.
            Dim allSucceeded As Boolean = True
            For Each result As MonitoringTaskResult In results.MonitoringTaskResults

                Console.WriteLine("Status: " & result.Status & " " & result.ErrorMessage)
                If (result.Status <> TaskStatus.Succeeded) Then
                    allSucceeded = False
                End If
            Next

            If (allSucceeded) Then
                Console.WriteLine("DeployAndInstallAgent succeeded.")
            Else
                Console.WriteLine("Not all agent installations succeeded.")
            End If

        End Function
    End Class
End Namespace

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.

' Repairs the agent on the specified agent-managed computer.
Imports System
Imports System.Collections.Generic
Imports System.Collections.ObjectModel
Imports System.Text
Imports Microsoft.EnterpriseManagement
Imports Microsoft.EnterpriseManagement.Administration
Imports Microsoft.EnterpriseManagement.Common
Imports Microsoft.EnterpriseManagement.Configuration
Imports Microsoft.EnterpriseManagement.Monitoring

Namespace SDKSamples
    Class Program
        Public Overloads Shared Function Main(ByVal args() As String) As Integer

            Dim mg As ManagementGroup = New ManagementGroup("localhost")

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

            ' Define the Management Server that manages the agent.
            Dim serverName As String = "EnterFullyQualifiedServerNameHere"

            Dim admin As ManagementGroupAdministration = mg.GetAdministration()

            Dim query As String = "Name = '" & serverName & "'"
            Dim serverCriteria As ManagementServerCriteria = New ManagementServerCriteria(query)

            Dim servers As ReadOnlyCollection(Of ManagementServer) = admin.GetManagementServers(serverCriteria)
            If (servers.Count <> 1) Then
                Throw New InvalidOperationException("Error! Expected one Management Server: " & query)
            End If

            ' Fully qualified name of the agent-managed computer.
            Dim fullAgentComputerName As String = "EnterAgentComputerNameHere"
            query = "Name = '" & fullAgentComputerName & "'"
            Dim agentCriteria As AgentManagedComputerCriteria = _
                New AgentManagedComputerCriteria(query)
            Dim agents As ReadOnlyCollection(Of AgentManagedComputer) = _
                admin.GetAgentManagedComputers(agentCriteria)
            If (agents.Count <> 1) Then
                Throw New InvalidOperationException("Error! Expected one managed computer with: " & query)
            End If

            Dim configuration As RepairAgentConfiguration = New RepairAgentConfiguration()

            ' Repair the agent.
            Dim results As AgentTaskResult = servers(0).RepairAgents(agents, configuration)

            Dim allSucceeded As Boolean = True
            For Each result As MonitoringTaskResult In results.MonitoringTaskResults

                Console.WriteLine("Status: " & result.Status & ", " & result.ErrorMessage)
                If (result.Status <> TaskStatus.Succeeded) Then
                    allSucceeded = False
                End If
            Next

            If (allSucceeded) Then
                Console.WriteLine("RepairAgent succeeded.")
            Else
                Console.WriteLine("Not all agents were repaired.")
            End If

        End Function
    End Class
End Namespace

See Also

Show: