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