Primary and Secondary Agent Failover

Yup your script should work - I’ve tidied it up a bit below just to make it a bit easier to enter going forward (you’ll need to run this again when you’ve added more agents in the future).

$agents = Get-SCOMAgent

$pri = Get-SCOMManagementServer -Name "SCOM01.madeup.com"
$sec = Get-SCOMManagementServer -Name "SCOM02.madeup.com"

$agents | where {$_.PrimaryManagementServerName -eq $pri.Name} | Set-SCOMParentManagementServer -PrimaryServer $pri -FailoverServer $sec
$agents | where {$_.PrimaryManagementServerName -eq $sec.Name} | Set-SCOMParentManagementServer -PrimaryServer $sec -FailoverServer $pri

Note for anyone who making changes to primary/failover management servers: the changes are sent to your agent via it’s management server, but they will usually receive and process the configuration change first. If an agent’s current MS (or one of the failovers) is not a new primary or failover server, it will very likely get locked out of the SCOM environment as it won’t know it now needs to communicate with a new management server and the previous ones will refuse to communicate with it.