Command Shell Examples
Useful SQL Queries
Discover and install agent using Powershell - Jonathan Almquist on Operations Manager - Site Home - TechNet Blogs

Discover and install agent using Powershell

Discover and install agent using Powershell

  • Comments 5
  • Likes

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.

I do not moderate this blog anymore. If you have a question regarding this post, send me a message.

Comments
  • Hey,

    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 Files\System Center Operations Manager\". I'm wondering how to change the installation path to "D:\Program Files\SystemCenter Operations Manager" I know it has something to do with Install-Agent -installAgentConfiguration, im just not sure how to change the installPath.

  • 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 here 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!

  • I get the following errors when running the above script:

    Connecting to Operations Manager Management Server 'N060SCE01.kroger.com'.

    PS Monitoring:\N060SCE01.kroger.com

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

    ssdev01)"

    PS Monitoring:\N060SCE01.kroger.com

    >$discoverycfg = New-WindowsDiscoveryConfiguration -ldapquery $query

    PS Monitoring:\N060SCE01.kroger.com

    >$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

    PS Monitoring:\N060SCE01.kroger.com

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

    coveryResults.CustomMonitoringObjects

    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

    PS Monitoring:\N060SCE01.kroger.com

    >

  • 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:\Temp\DiscoveredServers.txt"

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

    $InstallAgentConfiguration.InstallPath = "D:\Program Files\System Center Operations Manager 2007"

    # Install the agents that were discovered.

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

    #destroying used object.

    $InstallAgentConfiguration.Finalize

    Cheers,

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

    -Jonathan