Balancing Number of SCOM Agent Per Management Server using PowerShell

I came across a situation yesterday in one of the clients SCOM environment:

They currently have a single SCOM management group setup as the following:


  1. all SCOM management servers (including the root management server) are located on the same segment of the network.
  2. internal agents (from the same forest) are reporting to management server #1 and #2.
  3. External agents (from different forests) are reporting to management server #3 and #4 through firewall.
  4. SCOM is not integrated to AD – Therefore primary and failover management servers are not automatically assigned to agents.

I needed to achieve:

  1. agents are evenly distributed to the Internal and external management servers.
  2. all other management servers in the same group are assigned as failover management servers.
  3. For Example, there are 513 agent managed computers in internal network. They need to be evenly spread between management server #1 and #2 (so one management server will have 256 agents and the other will have 257 agents). All agents hanging off management server #1 will have #2 assigned as failover management server and vice versa.

I wrote this PowerShell script for this task. if you create a Windows scheduled task and run it on a regular basis, you’ll ensure all your SCOM agents are evenly assigned to a group of management servers and have correct settings for failover management servers.

The script does so by doing the following:

  1. Work out total number of agents that are currently hanging off the management servers specified in the script (line 33-37):image
  2. Work out which management servers are over average and which ones are below average
  3. Go through each one that’s over the average, move agents to another random management server until it reaches the average number.
  4. after each agent move, check the destination management server, make sure it is still under the average number, otherwise, remove it from the pool of under average management servers.
  5. Go through the remaining agents on each management server and make sure they are set to use all the other management servers as failover management servers.

Note: PowerShell Version 2 is required to run this script! This script can only run on the root management server. if you want to run it somewhere else, please modify line 15 of the script to have the correct FQDN of your RMS server.


  1. Tao, I’ve not used this script yet but it appears to be absolutely brilliant and helpful so thanks for putting the time into this one.

    Question: You declare/define this function below and pass it a variable named: “$PrimaryMS”
    at line 39:
    Function Get-FailoverMS ($PrimaryMS)
    $arrFailOverMSs = $Global:arrManagementServers
    Return $arrFailOverMSs

    Here is how you call the function, passing “$TargetMS” on line 76:
    $TargetMS = Get-Random -InputObject $arrMSLowerThanAverage
    $arrFailOverMSs = Get-failoverMS $TargetMS
    and again on line 89, passing “$MS”:
    $arrFailOverMSs = Get-failoverMS $MS

    The function declaration uses “$PrimaryMS” but the function body uses $TargetMS. Will you kindly clarify what seems to be an typo in the function declaration or body?

    Thanks for your time.

  2. Hi Tao,

    I’m trying to run this script in SCOM 2012 r2 environment.can you pls help me modify the script accordingly.

    pls let me know the changes to be made.


  3. I ran the PS script I dont think agents load is balance.
    agent 1 is on 3400
    agent 2 is on 1200
    agent 3 is on 400
    agent 4 is on 150
    can you please help me to fix the load balance

    1. someone else just had the same problem and asked me few days ago. all your agents must be configured as remote manageable before can be moved to other management servers. could you confirm if this is the case?

      1. 504 machines were on manual settings, i ran the sql query to change them
        running script again, will update you after script complete

  4. Thanks for the script. but what will happen to the agents which only connect to particular MS #3 and #4 ..they will get orphan if we add #1 and #2 as Primary MS.Please correct me if I am missing something.


Leave a Reply to Asim Khan Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: