Automatically Placing Servers in Maintenance Mode with SCOM during SCCM patching Reboot




In this post, I will walk you through automatically placing Servers in Maintenance Mode  during SCCM patching Reboot with a SCOM rule 

I have done this with a  SCOM rule, SCOM subscription, and Orchestrator runbook. in here ,I will only discuss the  SCOM rule. I will share the other methods in future postings.

1- Create a SCOM Alert based "Detects SCCM Reboot "in "Custom- Windows Server Monitoring " Management Pack

 Authoring | Rules| New Rule

Under Alert generating Rules Slect "NT Event Log" 




Name the rule 
For Target Select "Windows Computer "

Next Select System for Log Name


For Parameter See below

This is based on 
Event ID 1074
The process C:\Windows\CCM\CcmExec.exe (SERVERNAME) has initiated the restart of computer SERVERNAME on behalf of 
user NT AUTHORITY\SYSTEM



2- Now Tie an action script to run acommad that will place Server in Maintenance mode for X minutes.
Go to Authoring | Rules| and search for your new Rule
Open the new Rule 
Under Responses Add | Run Command

Place in Maintenance Mode



"D:\Scripts\Operations\MM_Mode.ps1" Start-serverScommaintenance -servername $Target/Property[Type="MicrosoftWindowsLibrary7585010!Microsoft.Windows.Computer"]/NetbiosComputerName$ -message "SCCM Patching/Reboot" -maintmodeinMinutes '30'


3- Create the MM_Mode.ps1 
-------------------------Below is the syntax for MM_Mode.ps1------------------------------

Param (
[string] $servername , 
       [string]$message, 
       [int]$maintModeinMinutes
       ) 
  Import-Module OperationsManager


    $funcName = 'func - Start-ServerScommaintenance:' 
    if(get-command -Name 'Get-SCOMClassInstance') 
    { 
        $server = (Get-SCOMClassInstance -DisplayName "$servername*") | select -first 1 | select -ExpandProperty Displayname 
        $scommanagementServers = (Get-SCOMManagementServer).displayName 
        if($scommanagementServers -ccontains $server) 
        { 
            Write-Warning "$funcname contains a Management Server $server.. You cannot put a management server in Maintenance Mode!!!" 
        } 
        else 
        { 
            $time = ((get-date).AddMinutes($maintModeinMinutes)) 
            $serverClassIds = Get-SCOMClassInstance -DisplayName $server 
            foreach($classid in $serverClassIds) 
            { 
                $server1 = Get-SCOMClassInstance -id ($classid.id) | Where-Object{$_.DisplayName -match $server} 
                write-host "$funcName putting " ($server1.id) ' in maintenance Mode Servername -->' ($Server1.DisplayName) 
                if(!(Get-SCOMMaintenanceMode -Instance $classid)) 
                { 
                    Start-SCOMMaintenanceMode -Instance $server1 -EndTime $time -reason PlannedOther -Comment $message 
                } 
                else 
                { Write-host "$funcname " $classid.id " has already been placed in Maintenance Mode"} 
            } 
        } 
    } 
    else 
    { Write-host "$funcname doesn't have the Operationsmanager module imported for this session"} 




  $File ="D:\Scripts\Operational\Live\MM_Mode\mm.txt"
#$File ="D:\Scripts\Operational\Live\SvcMgr_Ticketing\Logs\HPSM_Open_IM.txt"

"Alert Source"   | Out-File $File  -Append
$servername | Out-File $File  -Append
Get-Date | Out-File $File  -Append 

--------------------------------------------------End of Script------------------------------------------------------------

 Keep in mind that you can accompliush this with a  SCOM rule, SCOM subscription/channel, and Orchestrator runbook




Comments

  1. This looks like it executes the script from the Agent? Why not put the script in the Rule, so it's executed by the Management Server?

    ReplyDelete

Post a Comment

Popular posts from this blog

SCORCH 2016 Migration issue: an error occurred saving the activity to the data store Please check the orchestrator management service trace logs

Useful Orchestrator T-SQL Queries

Error in Orchestrator Web console and Web Service after moving Database