Set up automatic SCOM maintenance mode

Now this is something I am quite pleased with however after coming up with the idea and Googling I found plenty of other cleverer people had come up with this before me!

Here's one very good example:

http://operatingquadrant.com/2009/08/15/scom-automatically-starting-maintenance-mode-when-servers-are-rebooted-for-patching/

Anyways one of the maybe overlooked aspects of SCOM notification is that you can run commands, meaning that an event/alert can then trigger a series of scripted actions.  Really cool.

What the blog above explains is how to use this feature to check for an event signalling that the Windows server is going to reboot this is event ID 22 from Windows Update. 

I use event ID 1074 as we do not reboot our servers automatically when patching, we do however use Citrix and some of these servers have a reboot scheduled. 

Event 1074 is raised by USER32 in the SYSTEM log when a scheduled reboot is attempted.  So you can now safely put your servers into maintenance mode when they are scheduled to reboot.

The basic outline for acheiving this magic is relatively simple.
1. Create a management pack to monitor all servers for event 1074 (or/and 22)
2. Write some powershell to put a machine into maintenance mode.
3. Create a command notification to watch the monitor state and run the powershell when needed.

Here's the powershell:

#Connect to the RMS server and initialize the command shell
param($sHost)

trap [Exception]{
                $error = "Error " + $_Exception.Message;
}

$rmsServerName = "c6174.uk01.apmn.org"
Add-PSSnapin "Microsoft.EnterpriseManagement.OperationsManager.Client" -ErrorVariable errSnapin;
Set-Location "OperationsManagerMonitoring::" -ErrorVariable errSnapin;
new-managementGroupConnection -ConnectionString:$rmsServerName -ErrorVariable errSnapin;
set-location $rmsServerName -ErrorVariable errSnapin;

$time = [DateTime]::Now
$nMinutes = 20
$class = get-monitoringclass -name:Microsoft.Windows.Computer
$computerObj = $class | get-monitoringobject | where {$_.name -like “$sHost*”}

# write an event
$EventLog = new-object System.Diagnostics.EventLog('Application')
$EventType = [System.Diagnostics.EventLogEntryType]::Information

$EventLog.MachineName = "."
$EventLog.Source = "Maintenance Script Output"

$EventLog.WriteEntry("Result = $error`ntime = $time`nHost= $sHost`nClass = $class`nMonitor Object = $monObj",$EventType,1024)
New-MaintenanceWindow -MonitoringObject $computerObj -Comment “Planned Maintenance – scripted” -StartTime $time -EndTime $time.AddMinutes($nMinutes)

Comments

  1. Hi,
    Thanks for the post.

    Can you guide on the step 3 " Create a command notification to watch the monitor state and run the powershell when needed." please?

    ReplyDelete
  2. Srinivas thanks for the comment - sorry its taken so long to reply. I do not have access to SCOM anymore so but from what I remember creating a command notification is pretty straight forward...

    ReplyDelete

Post a Comment

Popular posts from this blog

PXE booting, MDT and 802.1x

Intune installation requires a wire...or does it?

Double vision...one GPO applying twice.