Maintenance Mode is an important concept to suppress alerts for planned maintenance windows. SCOM allows you to put all discovered in maintenance mode. This could be a Windows Computer, a SQL database or a distributed application. Depending on the activation of the maintenance mode, for a specific object, child objects are also put into maintenance mode automatically. For instance if a distributed application is put into maintenance mode, all components that belong to that DA are also put into maintenance mode. One of my customers had the requirement that when a computer is put into maintenance mode and this computer belongs to a cluster, the cluster object and therefore all the child objects (e.g. all cluster nodes) should be places into maintenance mode as well. I solved this with a PowerShell script.
The script can be used to put a computer into maintenance mode. The script checks if that computer is a cluster node. If true, the complete cluster with all nodes should be places into maintenance mode. If it’s a computer that does not belong to a cluster, only the computer itself is put into maintenance mode.
The script accepts different parameters, for instance the SCOM server name, the computer name to put into maintenance mode, the duration and a comment.
#
# System Center 2012 – Operations Manager
#
# Start Maintenance Mode Script For Computer Objects. If the Computer
# belongs to a Cluster, Maintenance Mode is started for the complete Cluster
# 2013, Marcel Zehner/itnetx
#param(
[Parameter(Position=1,mandatory=$true)][string]$scomservername,
[Parameter(Position=2,mandatory=$true)][string]$computername,
[Parameter(Position=3,mandatory=$true)][int]$hours,
[Parameter(Position=4,mandatory=$true)][string]$comment
)$error.clear()
import-module operationsmanager
# Connect to SCOM Server
new-scommanagementgroupconnection -computername $scomservername# Preparation
$start = get-date
$end = $start.addhours($hours)$class_wincomputer = Get-SCOMClass -name System.Computer
$class_wincluster = Get-SCOMClass -name Microsoft.Windows.Cluster
$relID = (Get-SCOMRelationship -name Microsoft.Windows.Cluster.Contains.Microsoft.Windows.Cluster.Node).id#Get Computer Object
$mmcomputerobject = get-scommonitoringobject -Class $class_wincomputer | where{$_.displayname -eq $computername}# Check If Object Exists
if(!$mmcomputerobject)
{
$output = “The computer was not found in SCOM”
Write-Host $output -foregroundcolor yellow
exit
}#Check if Computer belongs to a cluster
$clustername = $null
$clustername = (Get-SCOMRelationshipInstance -TargetInstance $mmcomputerobject | where{$_.relationshipid -eq $relID}).Sourceobject.name#Enable Maintenance Mode for the whole Cluster if it does belong to a Windows Cluster
if($clustername)
{
$mmclusterobject = Get-SCOMClassInstance -class $class_wincluster | where{$_.displayname -eq $clustername}# Check If the Cluster is already In Maintenance Mode
if($mmclusterobject.inmaintenancemode -eq $true)
{
$output = “The computer belongs to a cluster. The cluster is already in maintenance mode”
Write-Host $output -foregroundcolor yellow
}# Start Maintenance Mode For Cluster
if($mmclusterobject.inmaintenancemode -eq $false)
{
Start-SCOMMaintenanceMode -Instance $mmclusterobject -EndTime $end -Comment $comment# Check If Maintenance Mode Activation Was Successful
$mmclusterobject = Get-SCOMClassInstance -class $class_wincluster | where{$_.displayname -eq $clustername}
if($mmclusterobject.inmaintenancemode){$output = “The computer belongs to a cluster. The cluster is now in maintenance mode”}
else{$output = “The computer belongs to a cluster. An error occured while starting maintenance mode for the cluster”}
Write-Host $output -foregroundcolor yellow
}
}#Enable Maintenance Mode only for the Computer if it does not belong to a Windows Cluster
else{# Check If the Computer os already In Maintenance Mode
if($mmcomputerobject.inmaintenancemode -eq $true)
{
$output = “The computer does not belong to a cluster. The computer is already in maintenance mode”
Write-Host $output -foregroundcolor yellow
}if($mmcomputerobject.inmaintenancemode -eq $false)
{
Start-SCOMMaintenanceMode -Instance $mmcomputerobject -EndTime $end -Comment $comment# Check If Maintenance Mode Activation Was Successful
$mmcomputerobject = get-scommonitoringobject -Class $class_wincomputer | where{$_.displayname -eq $computername}
if($mmcomputerobject.inmaintenancemode){$output = “The computer does not belong to a cluster. The computer is now in maintenance mode”}
else{$output = “The computer does not belong to a cluster. An error occured while starting maintenance mode for the computer”}Write-Host $output -foregroundcolor yellow
}
}
Have fun with the script!
Cheers
Marcel
Pingback: SCOM 2012: Put Cluster Node and Cluster Object in Maintenance Mode - SysManBlog
Hi Marcel,
Nice script, it works pretty good.
Only small problem I have, is that I can only use “hours” and not minutes. One hour is to long for me, so can I use this script also with minutes?
Thanks,
Erik.
Hi,
The question above is solved, just replace “hours” with “minutes” in the whole script.
Other question I have (and im not a PS guru), is how can I let this script run automatically for specific variables? So I don’t want to fill in the “scomservername”, “computername”, “minutes” and “comment” manually all the time.
Any help very much appreciated!
Best,
Erik.
Don’t bother…. 🙂
http://www.powershell.nu/2009/12/16/running-scripts-with-arguments-in-powershell/
For example:
PS D:\Scripts> .\MM.ps1 “name of the scom server” “name of the server” “10” “test comment”
Hey Erik
Nice to hear you found out things!
Cheers
Marcel
Hi, I am trying to use this in my environment, but the script fails at this bit:
$mmcomputerobject = get-scommonitoringobject -Class $class_wincomputer | where{$_.displayname -eq $computername}
If I run it withouth the -Class, it comes up with the following info:
HealthState InMaintenanceMode DisplayName
———– —————– ———–
Success False MMTestClusterNode
is this the correct class object that needs to be used?
$class_wincomputer seems to be OK at this point:
PS C:\Windows\System32\WindowsPowerShell\v1.0> $class_wincomputer
DisplayName Name ManagementPackName
———– —- ——————
Windows Computer Microsoft.Windows.Computer Microsoft.Windows.Library
any thoughts?
Hi, I am trying to use this in my environment, but the script fails at this bit:
$mmcomputerobject = get-scommonitoringobject -Class $class_wincomputer | where{$_.displayname -eq $computername}
If I run it without the -Class, it comes up with the following info:
PS C:\Windows\System32\WindowsPowerShell\v1.0> get-scommonitoringobject | where{$_.displayname -eq $computername}
HealthState InMaintenanceMode DisplayName
———– —————– ———–
Success False MMTestClusterNode
is this the correct class object that needs to be used?
$class_wincomputer seems to be OK at this point:
PS C:\Windows\System32\WindowsPowerShell\v1.0> $class_wincomputer
DisplayName Name ManagementPackName
———– —- ——————
Windows Computer Microsoft.Windows.Computer Microsoft.Windows.Library
any thoughts?
Hello Eric, Marcel… can you help me by editing this script to take input for multiple servers from txt file