Balancing Number of SCOM Agent Per Management Server using PowerShell

Written by Tao Yang

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:

Drawing2

  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.

11 comments on “Balancing Number of SCOM Agent Per Management Server using PowerShell

  1. Nice Work. It very usefull

  2. Very Cool script, nice work.

  3. Worked like a charm and saved me alot of time, thanks for publishing this.

    Cheers!

  4. 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
    $arrFailOverMSs.Remove($TargetMS)
    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.
    Tyson

  5. 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.

    regards,
    Sameer

  6. 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

Leave a Reply