Putting a server in SCOM in maintenance mode.


 

As an avid scripter anytime I have to do something more than once in a gui. I dig for a way to make my job easier and script it. This is one example of something that I had to do repetitively that I chose to script.

Ever wanted to put a server in maintenance mode in your System Center Operations Manager Console via script with this script I’ll show you how to do that.

First let me show you the steps that we’d need to take manually to put a server in Scomm Maintenance mode.

clip_image002

Find your server to put in maint mode:

clip_image004

Putting each server or clicking each individual server into maintenance mode can be a very tedious task.

I’ll explain how this is done in the script that has been posted here: https://gallery.technet.microsoft.com/scriptcenter/Put-server-in-Maintenance-0a23e1fe

Function: Start-ServerScommaintenance 

This function expects a parameter of –servername whatever server you wish to put in maintenance mode.

-message this is the message that you expect to see in the scom console just like what you type in when you are putting a server in maintenance mode from the Scom console.

-maintModeinMinutes this is the amount of minutes that you are putting the server in maintenance mode.

clip_image006

Now lets talk about the rest of the script and how it functions:

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!!!”

        } 

The get-command verifies that you have initialized the SCOM server and the cmdlets are available for the function.

$server = (Get-SCOMClassInstance -DisplayName “$servername*”) | select -first 1 | select -ExpandProperty Displayname 

Get-SCOMClassInstance gets all the scom classes the name of the server you pass.

$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!!!”  }

   This command gets all your SCOM management servers. If $scommmanagementServers –ccontains your server name then we’ll write a message an exit.

Else we go into the routine of putting a server into 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”} 

$time contains the minutes we are going to put our server in maintenance mode.

$time = ((get-date).AddMinutes($maintModeinMinutes)) 

The Get-SCOMClassInstance gets the class instances for the server we passed in

PS C:\opsshell> Get-SCOMClassInstance -DisplayName “myserver*”

HealthState InMaintenanceMode DisplayName

———– —————– ———–

Success False myserver

Success False myserver

Success False myserver

Success False myserver

Warning False myserver

Warning False myserver

As you can see we have 6 clases tied to this server ‘myserver’

PS C:\opsshell> (Get-SCOMClassInstance -DisplayName “myserver*”).count

6

Next we’ll use the for each loop to go through each class to put it in maintenance mode.

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”} 

We’ll loop through each class id in Serverclassids setting Classid as our object that we are going to check.

$server1 will contain the class id of the first item in the Serverclassids array.

Then we will check to make sure it isn’t in maintenance mode. If it’s not we’ll start the maintenance mode with

Start-SCOMMaintenanceMode -Instance $server1 -EndTime $time -reason PlannedOther -Comment $message

If it is in maintenance mode Scom will error and tell you that it already put it in maintenance mode. Because of the relationships of classes in Scom it maybe put in maintenance mode when you attempt to put the very first class in. This is why there is a check to ensure that it hasn’t been put in maintenance mode yet.

function Stop-ServerScommaintenance 

The logic for the stop is nearly the same as the start. The only difference between the stop and the start is the time variable and the command shown below:

$result = (Get-SCOMClassInstance -id ($classid.id)|Where-Object{$_.Displayname -like $servername}).StopMaintenanceMode((get-date).ToUniversalTime()) 

    The function .StopMaintenanceMode expects a UTC time so we use GET-DATE and convert the time now to utc.

That about does it for these two functions.

thom        

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s