Friday, May 27, 2016

OpsMgr (SCOM) - (Bulk) Set Failover Management Server Powershell Script

I believe every SCOM Admin needs this.

This a script that for every agent you have, if it doesn't have a failover Management Server, it'll set it one.

NOTE : Please read comments inline before you run this

Enjoy!

 try{  
   [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.EnterpriseManagement.OperationsManager.Common") | Out-Null  
   [System.Reflection.Assembly]::LoadWithPartialName('Microsoft.EnterpriseManagement.Core') | Out-Null  
   [System.Reflection.Assembly]::LoadWithPartialName('Microsoft.EnterpriseManagement.OperationsManager') | Out-Null  
   [System.Reflection.Assembly]::LoadWithPartialName('Microsoft.EnterpriseManagement.Runtime') | Out-Null  
 } Catch { '[OpsMgr] - DLL could not be loaded' }  
 try{  
   $MGConnSetting = New-Object Microsoft.EnterpriseManagement.ManagementGroupConnectionSettings("$env:computername")  
   $MG = New-Object Microsoft.EnterpriseManagement.ManagementGroup($MGConnSetting)  
 } Catch { '[OpsMgr] - Could not connect to Management Group' }  
 # Let's say .. you want to have a list of MS Servers you dont want to work as failover (in my case i don't want Network RP MS servers ... - It's up to you!) :)  
 $IgnoredMS = @('Server1','Server2')  
 # Criteria for every MS you got  
 $MSCriteria = New-Object Microsoft.EnterpriseManagement.Administration.ManagementServerCriteria("Name LIKE '%'")  
 # Let's say you only want to work on specific criteria  
 $MSCriteria = New-Object Microsoft.EnterpriseManagement.Administration.ManagementServerCriteria("Name LIKE 'OpsMgrServer%'")  
 $ManagementServers = ($MG.Administration.GetManagementServers($MSCriteria)).DisplayName | ? { $_ -notin $IgnoredMS }  
 $AgentCriteria = New-Object Microsoft.EnterpriseManagement.Administration.AgentManagedComputerCriteria("Name LIKE '%'")  
 $Agents = $MG.Administration.GetAgentManagedComputers($AgentCriteria) | ? { $_.PrimaryManagementServerName -in @($ManagementServers) }  
 # My logic is :  
 #     - Specific agent will only have a failover MS with the same domain  
 #    So you may need to edit code before running this.  
 Foreach ( $agent in $Agents ) {  
   If ( !($agent.GetFailoverManagementServers()) ) {  
     $PrimaryMS = $agent.PrimaryManagementServerName  
     $Domain = ($PrimaryMS -split "\.")[-2..-1] -join '.'  
     $FailoverMS = ($ManagementServers | Select-String -Pattern "$Domain" | ? { $_ -notin $PrimaryMS })[0]  
     $PrimaryMSCriteria = New-Object Microsoft.EnterpriseManagement.Administration.ManagementServerCriteria("Name = '$PrimaryMS'")  
     $PrimaryManagementServerID = $MG.Administration.GetManagementServers($PrimaryMSCriteria).ID  
     $PrimaryManagementServer = $MG.Administration.GetManagementServer($PrimaryManagementServerID)  
     $FailoverMSCriteria = New-Object Microsoft.EnterpriseManagement.Administration.ManagementServerCriteria("Name = '$FailoverMS'")  
     $FailoverMSIList = New-Object 'Collections.Generic.List[Microsoft.EnterpriseManagement.Administration.ManagementServer]'  
     $MG.Administration.GetManagementServers($FailoverMSCriteria) | % { $FailoverMSIList.Add($_) }  
     $agent.SetManagementServers($PrimaryManagementServer, $FailoverMSIList)  
   } Else { "$(($agent).DisplayName)" + ' has already a Failover MS configured' + "$($agent.GetFailoverManagementServers().DisplayName)" }  
 }  

Cheers!

No comments:

Post a Comment