SCOM Maintenance Mode PowerShell


My thanks to Matt Taylor and Kevin Holman for their guidance!

 

Read on if these apply
Trying to start, update, or end SCOM MM

Get alerts when MM is updated
PowerShell only in your shop!
SCORCH in play but need to convert runbooks to straight PowerShell

Ran into issues using Set-SCOMMaintenanceMode, as the cmdlet doesn't put ALL the recursive classes under Windows Computer

 

 

Background

Set-SCOMMaintenanceMode cmdlet is actually “by design.”  ☹

 

Start-SCOMMaintenanceMode assumes you want recursive action when you start maintenance mode…. So you pick a Windows Computer and it places the Windows Computer object (AND all contained objects) into MM.

Computer in MM

All contained objects in MM

 

 

However, the problem is that Set-SCOMMaintenancemode does not have an understanding of recursiveness.  It changes the MM entry for the Windows Computer, but NOT all the contained objects.  So they retain the original setting.

 

Health explorer looks like this, resulting in unwanted alerts

 

 

 

Details

NOTE these $Time and DateTime Method are dependent on the delay between running the commands
If you start MM, and wait 5 minutes, then update, the total MM duration will be ~20 minutes

 

# Start MM

$server = "Servername.FQDN"

$instance = (get-scomclass -DisplayName "Windows Computer" |Get-SCOMClassInstance | ? { $_.DisplayName -eq $server } )
$Time = (Get-Date).addMinutes(6)
Start-SCOMMaintenanceMode -Instance $Instance -EndTime $Time -Comment "Starting Maintenance Mode." -Reason "PlannedOther"

 

# Update MM
# Make sure you've put object in MM
$guid = $instance.id
$WCobj = Get-SCOMMonitoringObject -Id $guid

# 15 minutes in the future
$WCobj.UpdateMaintenanceMode([System.datetime]::Now.addminutes(15).touniversaltime(),[Microsoft.EnterpriseManagement.Monitoring.MaintenanceModeReason]::PlannedOther,[System.string]::"Adding 15 minutes to the end time.",[Microsoft.EnterpriseManagement.Common.TraversalDepth]::Recursive);

 

# Stop MM
# Make sure you've put object in MM

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

# 15 minutes in the future
$WCobj.StopMaintenanceMode([System.DateTime]::Now.addminutes(15).touniversaltime(),Microsoft.EnterpriseManagement.Common.TraversalDepth]::Recursive);

# 1 hour in the future
$WCobj.StopMaintenanceMode([System.DateTime]::Now.addhours(1).touniversaltime(),Microsoft.EnterpriseManagement.Common.TraversalDepth]::Recursive);

 

# Validate MM through Operations Manager Event ID’s 1215 and 1216 logged

get-eventlog -LogName "Operations Manager" | ? { $_.EventID -eq 1215 -OR $_.EventID -eq 1216 } |fl EventID,TimeGenerated,Message

 

 

# Error if object NOT in MM

Cannot find an overload for "UpdateMaintenanceMode" and the argument count: "1".

At line:1 char:1

+ $WCobj.UpdateMaintenanceMode(([System.datetime]::Now).addminutes(15). ...

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

    + CategoryInfo          : NotSpecified: (:) [], MethodException

    + FullyQualifiedErrorId : MethodCountCouldNotFindBest

 

PS C:\Windows\system32>

 

Testing System datetime

PS C:\Windows\system32> [System.datetime]::Now.addminutes(15)

 

Thursday, August 24, 2017 9:18:04 AM

 

 

PS C:\Windows\system32> ([System.datetime]::Now.addminutes(15)).touniversaltime()

 

Thursday, August 24, 2017 2:18:16 PM

 

 

 

 

References

SDK

DateTime Methods https://msdn.microsoft.com/en-us/library/system.datetime_methods(v=vs.110).aspx

MaintenanceModeReason Method https://msdn.microsoft.com/en-us/library/microsoft.enterprisemanagement.monitoring.maintenancemodereason.aspx

StopMaintenanceMode Method https://msdn.microsoft.com/en-us/library/microsoft.enterprisemanagement.monitoring.partialmonitoringobject.stopmaintenancemode.aspx

UpdateMaintenanceMode Method https://msdn.microsoft.com/en-us/library/bb424495.aspx

 

MM deluxe custom script https://gist.github.com/stegenfeldt/b3f044aa77894ed80d82f8849a48035b

Comments (0)

Skip to main content