Discover and install agent using Powershell

Here's an example of how you can run discovery and agent installation using the OpsMgr Command Shell.

$query = New-LdapQueryDiscoveryCriteria -domain <your_domain> -ldapquery "(cn=<target_netbios_name>)"

$discoverycfg = New-WindowsDiscoveryConfiguration -ldapquery $query

$discoveryResults = Start-Discovery -managementServer (get-managementServer) -windowsDiscoveryConfiguration $discoverycfg

Install-Agent -managementServer (get-ManagementServer) -agentManagedComputer $discoveryResults.CustomMonitoringObjects

Note: This will configure the agent to report to the Management Server you are currently connected to while performing this action.

Comments (5)

  1. Good question, Kyle.  I’m not familiar with this installation parameter, but will look into how this is used.

    Essentially, this requires some knowledge of creating objects and .Net Framework.  There is a reference

     for this class, but I don’t see any specific examples of creating an installation path.

    If I figure this out, I’ll post an update here.  If anyone else had figured out how to use this parameter, please do share!

  2. Thanks for the update, Kyle.  Hopefully others in the community will get some use out of this.


  3. Kyle says:


    I am wondering if there is a way to choose the installation directory on the machine the agent is being deployed to. The Default is "C:Porgram FilesSystem Center Operations Manager". I’m wondering how to change the installation path to "D:Program FilesSystemCenter Operations Manager" I know it has something to do with Install-Agent -installAgentConfiguration, im just not sure how to change the installPath.

  4. dan wiley says:

    I get the following errors when running the above script:

    Connecting to Operations Manager Management Server ''.


    >$query = New-LdapQueryDiscoveryCriteria -domain -ldapquery "(cn=n060mo



    >$discoverycfg = New-WindowsDiscoveryConfiguration -ldapquery $query


    >$discoveryResults = Start-Discovery -managementServer (get-managementServer) -win

    dowsDiscoveryConfiguration $discoverycfg

    Start-Discovery : Cannot convert 'System.Object[]' to the type 'Microsoft.Enterpr

    iseManagement.Administration.ManagementServer' required by parameter 'ManagementS

    erver'. Specified method is not supported.

    At line:1 char:54

    + $discoveryResults = Start-Discovery -managementServer  <<<< (get-managementServ

    er) -windowsDiscoveryConfiguration $discoverycfg


    >Install-Agent -managementServer (get-ManagementServer) -agentManagedComputer $dis


    Install-Agent : Cannot convert 'System.Object[]' to the type 'Microsoft.Enterpris

    eManagement.Administration.ManagementServer' required by parameter 'ManagementSer

    ver'. Specified method is not supported.

    At line:1 char:32

    + Install-Agent -managementServer  <<<< (get-ManagementServer) -agentManagedCompu

    ter $discoveryResults.CustomMonitoringObjects



  5. Kyle says:

    Hello Jtalmquist,

    Below is the answer to the problem 🙂

    $rootMS = "servername.domainname"

    # Initialize the OpsMgr Provider

    Add-PSSnapin Microsoft.EnterpriseManagement.OperationsManager.Client

    # Set the location to the root of the provider namespace.

    cd OperationsManagerMonitoring::

    #create a connection to the Management Group

    New-ManagementGroupConnection $rootMS

    #change the path

    cd $rootMS

    # Get the Root Management Server Object

    $rootManagementServer = Get-ManagementServer

    # Create a variable to hold the domain name

    $Domain = "DomainName"

    # Create the query that will be used to search for all servers to discover

    $LDAPQuery = New-LdapQueryDiscoveryCriteria -domain $Domain -ldapquery "(&(operatingSystem=*Server*)(!cn=server1)(!cn=server2)(!cn=server3)(!cn=server4))"

    # Set up a new discovery config

    $discoveryConfig =  New-WindowsDiscoveryConfiguration -ldapquery $LDAPQuery

    # Start the DIscovery and add results to $DiscoveryResult

    $discoveryResult = Start-Discovery -ManagementServer: $rootManagementServer –WindowsDiscoveryConfiguration: $discoveryConfig

    $discoveryResult.CustomMonitoringObjects > "C:TempDiscoveredServers.txt"

    $InstallAgentConfiguration = New-Object Microsoft.EnterpriseManagement.Administration.InstallAgentConfiguration

    $InstallAgentConfiguration.InstallPath = "D:Program FilesSystem Center Operations Manager 2007"

    # Install the agents that were discovered.

    Install-Agent –ManagementServer: $rootManagementServer –AgentManagedComputer: $discoveryResult.CustomMonitoringObjects -InstallAgentConfiguration: $InstallAgentConfiguration

    #destroying used object.



Skip to main content