So we’ve recently stood up SCOM 2012 and after weeks of planning we deployed it into our environment. We used SCCM 2012 to push out clients, which isn’t as well supported as I would like, considering they’re sister components of Microsoft System Center, but I digress. Our application deployment in SCCM pointed to one of our two management servers, which rapidly began to fill up with clients, while the other management server sat idle. I wasn’t about to start moving clients manually, so I set out to automate the process of load balancing agents between management servers on a nightly basis.
It didn’t take much time on Google to find several different Powershell scripts that would load balance agents between management servers, but I noticed one big flaw – they only worked if you had two management servers. Granted, our environment only has two but if we were to add a third, I don’t want to have to redo the script, so I decided to come up with a script that would accommodate any number of management servers. The script below will load balance all agents between management servers within the specified resource pool. This is the script I use in Orchestrator, with the logging and error checking stripped out for clarity and interoperability.
The Script
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# Path to Operations Manager 2012 R2 Module $module_path = "C:\Program Files\Microsoft System Center 2012 R2\Operations Manager\Powershell\OperationsManager\OperationsManager.psm1" # SCOM Server to connect to $scom_server = "SCOMMGT1.lab.local" # Name of Resource Pool to retrieve Management Servers from $resource_pool_name = "All Management Servers Resource Pool" # Clear out runtime variables $mgt_servers = @() # Attempt to import SCOM PS Module from $module_path Import-Module $module_path -ErrorAction Stop # Connect to Management Group New-SCOMManagementGroupConnection -ComputerName $scom_server # Get the All Management Servers resource pool $resource_pool = Get-SCOMResourcePool -Name $resource_pool_name # Get management server objects foreach ($member in $resource_pool.Members) { # Retrieve Management Server Objects in specified resource pool. $mgt_servers += Get-SCOMManagementServer -Name $member.DisplayName } # Get all of the SCOM agents $agents = Get-SCOMAgent #Iterate through all agents foreach ($agent in $agents) { # Split out mgt server array to store first server, and then rest in $other_mgts $primary_mgt, $other_mgts = $mgt_servers # Assign secondary management server to variable $secondary_mgt = $mgt_servers[1] # Clear out mgt_servers $mgt_servers = @() # Clear out failover management server Set-SCOMParentManagementServer -Agent $agent -FailoverServer $null #Set primary management server Set-SCOMParentManagementServer -Agent $agent -PrimaryServer $primary_mgt # Set failover management server Set-SCOMParentManagementServer -Agent $agent -FailoverServer $secondary_mgt # Push primary onto the end of the array for the next loop $mgt_servers += $other_mgts $mgt_servers += $primary_mgt } |
How it works
It assigns all management servers to an array, takes the management server at array index 0 and sets it as the primary server, then index 1 as the secondary server. After assigning the failover and primary servers on the agent, it reorders the array so that all management servers at index 1 and above are on top, then tacks what used to be index 0 onto the end. Then the process repeats, creating a queue from which servers will be assigned. Be aware that this script requires that you have the Operations Manager Shell installed, which is installed as part of the Operations Manager Console.
You can then put this into a Run .NET Script activity in Orchestrator, a Scheduled Task on a server, or just run it periodically. Personally, I use Orchestrator to run this in the early morning everday, and it creates a SCOM notification on success, and a SCOM alert on failure, like so:
For information on how to automate Orchestrator 2012 runbooks with Scheduled Tasks, check out my blog post Automating System Center Orchestrator 2012 Runbooks with Scheduled Tasks. Happy automating!
Hi Ryan,
great Script which works like charm. Thanks.
But i have a little different scenario.
We have 3 management server in the resource pool, one of the management servers will be used for ACS. i want to load balance only between the other 2 management server. Pls suggest what changes needs to be done in the script.
BR,
Sameer
Hey Sameer,
If you want to load balance all agents across your other two non-ACS management servers, all you have to do is create a new Resource Pool that contains only those two management servers then change this line to reflect the display name of your new resource pool.
Cheers!