OpsMgr by Example: Automatically Balancing the Windows Agent Load with PowerShell

Special thanks to OpsMgr community guru Andreas Zuckerhut for contributing this awesome OpsMgr Command Shell script to eliminate a common challenge in large OpsMgr environments!

Of all the functions made easier by the new resource pools feature in System Center 2012 Operations Manager (OpsMgr), evenly distributing agent load is not one of them. If you perform a push installation of 500 agents in one pass, all 500 report to the same primary management server. In enterprises where the OpsMgr agent is incorporated into the operating system image but Active Directory integration for agent assignment is not in place, distributing the agent load evenly across multiple management servers can be a real headache.

So what can you do if you have multiple management servers and do not want to use the OpsMgr AD integration feature?

Turn to PowerShell.

While it’s relatively easy to balance agents across two management servers with PowerShell, the script logic becomes significantly more complex when you need to support 2–N management servers (where N is the max supported limit in OpsMgr, currently 10). Fortunately, that did not bother Andreas, who routinely writes PowerShell-based automation solutions for OpsMgr that rate at the high end of the complexity scale.

This solution will be great as a Windows Scheduled Task, and could be run from Orchestrator on-demand (don’t use Orchestrator as an enterprise scheduler!)

Sample Script

This sample script (which consists of three PowerShell functions) redistributes agents across a user-specified list of management servers, performing the following high level steps:

  • Attempts to connect to all management servers specified in the script parameters
  • Retrieves the agents reporting to these management servers
  • Calculates the minimum agents per management server (based on the number of management servers specified in the script parameters and associated agent count)
  • Reassigns the primary management server of the agents in a even fashion across the specified management servers

It also includes good error handling (in case an management server does not exist or is unreachable) as well as some logic to identify when only one management server has been specified (and thus no agent redistribution can be performed).


The script requires two parameters:

  • The first is a semi-colon separated list of management servers across which to distribute the agent load
  • The second is a the name of a management server to use for the OpsMgr SDK connection

For Example:

redistribute agents.ps1 “Hannibal.odyssey.com;Hector.odyssey.com;helios.odyssey.com” “localhost”


The script reports its progress at each step from connecting to management server, retrieving reporting agents and agent counts, and the result of each agent assignment, as shown in this image.


The sample script is shown below.


To quote Peter Parker’s (aka Spiderman) uncle, “With great power comes great responsibility”. With that being said, make sure you heed the following advice BEFORE using this script in a production environment.

  • Test the script thoroughly in a development environment (with multiple management servers)
  • Do NOT include management servers that are not network accessible to all agent managed computers reporting to the management servers you specify in the script parameters
  • Validate the results of your testing by looking at the agent distribution before and after the script runs. You can check this in the Operations console in the Administration pane –> Agent Managed node.



One thought on “OpsMgr by Example: Automatically Balancing the Windows Agent Load with PowerShell

  1. imvincent


    It seems that the script is not complete. Would anyone help me complete this script? I’m not a scripting guy. 🙁

    Thanks in advance.

Leave a Reply

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