SCOM 2012 R2 - Use PowerShell to end an active maintenance mode

The following information was inspired by another post by one of my colleagues, Neil Peterson.  Last year, Neil put up a great post called Integrating Cluster Aware Updating with Operations Manager Maintenance Mode – Pre and Post Update Scripts

In that post, Neil explained how we could use PowerShell to place cluster nodes into maintenance mode in Operations Manager when a Cluster Aware Update job was run.  Neil looked at placing a cluster node into Operations Manager maintenance mode, and in this post I will show how we can take a node out of maintenance mode once it has finished applying updates.

In the screenshot from Neil’s post below, we see that he setup an Updating Run Profile that specified a PowerShell PS1 file to act as a PreUpdateScript.  This PS1 file was written to place the node being updated by Cluster Aware Updating into Operations Manager maintenance mode.  For our purposes, we will show how to create a new PS1 file to take a node out of maintenance mode once updating has completed.  Our goal is to restore monitoring on the cluster node as soon as possible once updating has finished for that node.  We would look to include this new PS1 file as our PostUpdateScript in our Updating Run Profile.  Without the PostUpdateScript, the node will stay in maintenance mode for the entire period that was specified in the PreUpdateScript, unless an administrator ends the maintenance mode early.  Through this example we look to show how that can be handled automatically.

Looking at the PowerShell code provided by Neil, I saw it included the start-scommaintenancemode cmdlet.  So in my first attempts to setup the PostUpdateScript, I looked to see if I could find a stop-scommaintenancemode cmdlet.  Sadly it was not that easy, as such a cmdlet does not exist.  But the silver lining is that it gave me a reason to put together this post, and hopefully help others out looking for a solution to this.

Through some research, I learned that while there is not a stop-scommaintenancemode cmdlet, there is a StopMaintenanceMode method.  Using the following example from Boris Yanushpolsky for SCOM 2007, I found that the StopMaintenanceMode method is still valid in Operations Manager 2012 R2, and I was able to borrow the following code from Boris’ post:

$computer.StopMaintenanceMode([System.DateTime]::Now.ToUniversalTime(),[Microsoft.EnterpriseManagement.Common.TraversalDepth]::Recursive);

This method allowed me to end an active Operations Manager maintenance mode immediately, and the recursive setting ensured that all monitors underneath the Windows Computer object exited maintenance mode as well.

Full code to my PostUpdateScript PS1 file:

$secpasswd = ConvertTo-SecureString "<Password>" -AsPlainText -Force

$mycreds = New-Object System.Management.Automation.PSCredential ("<DOMAIN\user>", $secpasswd)

$ComputerName = [System.Net.Dns]::GetHostByName(($env:computerName)).HostName.ToString()

Invoke-Command -ComputerName <SCOMServer> -Credential $mycreds -ScriptBlock {

   import-module operationsmanager

   $class = get-scomclass -name:’Microsoft.Windows.Computer’

   $computer = get-scomclassinstance -class $class | where{$_.name -eq $using:ComputerName}

   $computer.StopMaintenanceMode([DateTime]::Now.ToUniversalTime(),[Microsoft.EnterpriseManagement.Common.TraversalDepth]::Recursive);

}

This code can then be saved as a PS1 file, and specified in an Updating Run Profile as a PostUpdateScript.  This is just a small example of what is possible using PowerShell to customize updates via the Cluster Aware Updates option, but I hope you found it useful!