Share via


PowerShell Script to place SCOM 2012 SP1 agent(s) in Maintenance Mode

I had to help a customer to place his Citrix servers in Maintenance Mode during the routine reboot.
He was trying to use the example in the MSDN and it was not working throwing the error bellow
As it turns out you have to get an instance of the <Microsoft.Windows.Computer> class for it to work.

-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-

Start-SCOMMaintenanceMode : The client has been disconnected from the server. Please call ManagementGroup.Reconnect() to reestablish the connection.

At C:\Scripts\SCOMAGENTMM.ps1:15 char:5

+ Start-SCOMMaintenanceMode -Instance $instance -EndTime $Time -Reason $reason ...

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

+ CategoryInfo : InvalidOperation: (Microsoft.Syste...anceModeCommand:StartSCMaintenanceModeCommand) [Start-SCOMMaintenanceMode], ServerDisconnectedE

xception

+ FullyQualifiedErrorId : ExecutionError,Microsoft.SystemCenter.OperationsManagerV10.Commands.StartSCMaintenanceModeCommand

-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-

<#

#>

So instead of just that:

$Instance = Get-SCOMClassInstance -Name $server

He had to use those lines on his script:

$instanceclass= get-scomclass -Name Microsoft.Windows.Computer
$instance = Get-SCOMClassInstance -Class $instanceclass | Where {$_.DisplayNAme -match $server}

<#

#>

So here it goes the complete PS1 script working on SCOM 2012 SP1 Managemet Server

Paste it on the new PowerShell ISE script window replacing computer names and done!

New-SCOMManagementGroupConnection -computername .

$servers = ('MyServer1.MyDomain.com','MyServer2.MyDomain.com','MyServer3.MyDomain.com')

$time=((Get-Date).AddMinutes(6))

$comment ="Citrix reboot routine"

$reason = "PlannedApplicationMaintenance"

foreach ($server in $servers)

{

$instanceclass= get-scomclass -Name Microsoft.Windows.Computer

$instance = Get-SCOMClassInstance -Class $instanceclass | Where {$_.DisplayNAme -match $server}

Write-Host $instance

Start-SCOMMaintenanceMode -Instance $instance -EndTime $Time -Comment $comment -Reason $reason

}