SCOM2012R2 - Put Agents behind Gateway in Maintenance Mode

asked 2020-09-08T05:04:53.623+00:00
saiyad rahim 21 Reputation points

I have a need to shut down a scom Gateway from old vCenter to new vCenter Environment.
To do this I need to put all Agents behind this Gateway in Maintenance Mode.

Have been trying with this Pshell but didn't work:

Import-Module OperationsManager

$Time = ((Get-Date).AddMinutes(10))
Get-SCOMAgent | where {$_.PrimaryManagementServerName -eq 'GatewayServerName'}|Select DisplayName | Get-SCOMClassInstance | Start-SCOMMaintenanceMode -passthru -EndTime $Time -Comment "Server Migration."

Can someone help in identifying what is a best way to put all Agents behind a Gateway/Management Server in to MM mode.

Operations Manager
Operations Manager
A family of System Center products that provide infrastructure monitoring, help ensure the predictable performance and availability of vital applications, and offer comprehensive monitoring for datacenters and cloud, both private and public.
1,199 questions
Microsoft System Center
Microsoft System Center
A suite of Microsoft systems management products that offer solutions for managing datacenter resources, private clouds, and client devices.
257 questions
No comments
{count} votes

Accepted answer
  1. answered 2020-09-08T06:45:54.923+00:00
    Leon Laude 84,981 Reputation points

    Hi,

    I've created a script below that should be what you're looking for:

    Import-Module OperationsManager
    
    $Time = ((Get-Date).AddMinutes(10))
    $ErrorActionPreference = 'SilentlyContinue'
    $InstanceClass = Get-SCOMClass -Name "Microsoft.Windows.Computer"
    $Agents = Get-SCOMAgent | where {$_.PrimaryManagementServerName -eq 'Gateway server name'} | Select DisplayName
    
    Foreach($Agent in $Agents){
        $Instance = Get-SCOMClassInstance -Class $InstanceClass | Where-Object {$_.DisplayName -match $Agent.DisplayName}
        Start-SCOMMaintenanceMode -Instance $Instance -EndTime $Time -Comment "Server Migration." -Reason "PlannedOther"
    
            #Checking Maintenance Mode
            $Agent
            Get-SCOMMaintenanceMode -Instance $Instance | select ManagementGroup,User,StartTime,ScheduledEndTime,Reason,Comments | fl
    

    }

    (If the reply was helpful please don't forget to upvote or accept as answer, thank you)

    Best regards,
    Leon

    No comments

1 additional answer

Sort by: Most helpful
  1. answered 2020-09-09T23:34:15.17+00:00
    saiyad rahim 21 Reputation points

    Hi Leon,
    That worked perfectly.

    Need your help in modifying this if I want to let the user choose which Gateway/Mgmt Server to target.

    If i put my values in a variable like this:

    $NthGateway="Server1"
    $SthGateway="Server2"
    $PrimaryMgtServer="Server2"
    $SecondaryMgtServer="Server4"

    Have the script ask:

    "Select SCOM Mgmt/Gateway"

     Press '1'  for Server1"
     Press '2'  for Server2"
     Press '3'  for Server3"
     Press '4'  for Server4"
    

    Would you be able to help me out with a proper script to do this?