Friday, June 22, 2012

[OpsMgr 2012] Agent Failover – Simple Powershell Script with Wildcards

I've found this post on http://www.teknoglot.se/ very interesting and also here is what they explain on it :

The scenario we are building this script for is that now and then you want to make sure that certain agents have fail-over management servers configured. You also want to make sure that all management servers that are not the primary management server of any selected agent will be in that list of fail-over servers. This would include any new management servers as well as exclude any removed ones. In short, make sure your agent fail-over settings are up-to-date with the current environment.

read more on http://www.teknoglot.se/code/powershell/opsmgr-2012-agent-failover-simple-script-with-wildcards-opsmgr-powershell/


The Copy/Paste Part

As usual, here’s the entire script for you to copy. Read it, try it, adapt it…
  1. # Input SCOM Management Server to connect to in this session
  2. [string]$inputScomMS = "scomms02.domain.local"
  3. # Input an existing agent you want to modify
  4. [string]$inputTargetAgent = "*.domain.local"
  5.    
  6. ### Connect to SCOM 2012 Management Group ###
  7. # Check if OperationsManager module is loaded
  8. If (Get-Module -Name "OperationsManager") {
  9.  try {
  10.  # Try to load the module
  11.  Import-Module -Name "OperationsManager"
  12.  } catch {
  13.  # Did not work, exit the script
  14.  echo "Could not load Operations Manager module"
  15.  exit
  16.  }
  17. }
  18. # Module is loaded, connect to Management Group/Server
  19. New-SCOMManagementGroupConnection -ComputerName $inputScomMS
  20. ### END ###
  21.    
  22. # Select matching remotely manageable agents
  23. $scomAgents = Get-SCOMAgent -DNSHostName $inputTargetAgent | Where {$_.ManuallyInstalled -ne $true}
  24. # Get all management servers
  25. $scomManagementServers = Get-SCOMManagementServer
  26. foreach ($scomAgent in $scomAgents) {
  27.  # Get the primary management server for the agent
  28.  $scomPrimaryMS = $scomManagementServers | where {$_.Name -eq $scomAgent.PrimaryManagementServerName}
  29.  # Remove the primary MS from "all MS" array to use it for FailOver servers
  30.  $scomFailOverMS = $scomManagementServers | Where-Object {$_.Name -ne $scomPrimaryMS.Name}
  31.  # Set Primary Management Server
  32.  Set-SCOMParentManagementServer -Agent $scomAgent -PrimaryServer $scomPrimaryMS
  33.  # Set Fail-over Management Server
  34.  Set-SCOMParentManagementServer -Agent $scomAgent -FailoverServer $scomFailOverMS
   
 

This posting is provided "AS IS" with no warranties.

No comments:

Post a Comment