This is one of the idea I got from one of my customer where he would like to know if any object for a particular server already in maintenance mode, however when they are doing any maintenance activity, they put entire server into maintenance mode. Due to this, once the maintenance mode will over, entire server and relationship objects will be coming out of maintenance mode. So I though of something to keep the data store while we will put the server into maintenance mode and later we will put only those objects in to maintenance mode again. This below 2 Scripts will do exactly the same and by doing this, we don’t even need to remember which objects we have put into maintenance mode. I hope this will help you. 

I would appreciate if you could provide some feedback in this.

What does this Scripts:

 

MMScriptStart.ps1:

 

##################################################################

# Author: Sourav Mahato

# Created Date:09/21/2019

<#Purpose: This script will first check if any objects already in maintenance mode in SCOM or not for a server. If any object in
maintenance mode, this script will fetch the information from SCOM and kept it in AlreadyMM.txt file And
then it will put entire server in maintenance mode. #>

# You need to copy this .PS1 file to a location on your Management Server.

# Now create two text file AlreadyMM.txt" and "Servers.txt" on the same directory.

# Now you can put the servers name one after another in Servers.txt file and execute the Script.

# You need to modify the $minutes = "60" section for how long you want to put the server into maintenance mode

##################################################################

Import-Module OperationsManager

$cur = get-location

Clear-Content "$cur\AlreadyMM.txt"

$Servers = Get-content "$cur\Servers.txt"

$minutes = "60"

$MonitoringObject = Get-SCOMClass -Name "Microsoft.Windows.Computer" | Get-SCOMClassInstance

foreach($Server in $Servers)

{

$ObjectInstance = Get-SCOMClassInstance -Name $Server

$Objects =$ObjectInstance.GetMonitoringRelationshipObjects()

$AllObjects = $Objects.TargetObject

$alreadyinmm = $AllObjects | ? {$_.InMaintenanceMode -eq $true}

$alreadyinmm.Id.Guid >>"$cur\AlreadyMM.txt"

$Object = $MonitoringObject | where {$_.displayname -like "$Server"}

$startTime = [System.DateTime]::Now

$endTime = $startTime.AddMinutes($minutes)

if($Object.InMaintenanceMode -eq $false)

{

Write-Host "Putting $Object.Displayname into maintenance mode for $endTime Minutes" -ForegroundColor Yellow

Start-SCOMMaintenanceMode -Instance $Object -EndTime:$EndTime -Reason: "PlannedOther" -Comment: "scheduled Activity - daily Reboot"

}

else

{

Write-Host "Server $Object.Displayname is already into maintenance mode" -ForegroundColor Green

}

}

 

This script will first check if any objects already in maintenance mode in SCOM or not for a server. If any object in maintenance mode, this script will fetch the information from SCOM and kept it in AlreadyMM.txt file. And then it will put entire server in maintenance mode.

 

Please get the Script from here: https://github.com/souravmahato7/Codes/blob/SCOM/MMScriptStart.ps1

 

Remove-RestoreMM.ps1:

 

##################################################################

# Author: Sourav Mahato

# Created Date:09/21/2019

<#Purpose: This script will first check if the server is in maintenance mode or not. If the server is in maintenance mode,
it will remove the server from maintenance mode and restore the maintenance mode for the objects which were previously in
maintenance mode in SCOM. Also, if the maintenance mode is already ended by the maintenance schedule, it will restore the
maintenance mode for the objects which were previously in maintenance mode in SCOM. #>

# You need to copy this .PS1 file to a location on your Management Server.

# Now create two text file AlreadyMM.txt" and "Servers.txt" on the same directory.

# Now you can put the servers name one after another in Servers.txt file and execute the Script.

# You need to modify the $minutes = "60" section for how long you want to put the server into maintenance mode

##################################################################

Import-Module OperationsManager

$cur = get-location

$Servers = Get-content "$cur\Servers.txt"

$minutes = "518400"

$time = [DateTime]::Now

$alreadyinmmformember = Get-Content "$Cur\AlreadyMM.txt"

$MonitoringObject = Get-SCOMClass -Name "Microsoft.Windows.Computer" | Get-SCOMClassInstance

foreach($member in $Servers)

{
$Object = $MonitoringObject | where {$_.displayname -like "$member"}

if($Object.InMaintenanceMode -eq $True)

{

Write-Host "stopping maintenance for $member" -ForegroundColor Green

$Object.StopMaintenanceMode([DateTime]::Now.ToUniversalTime(),[Microsoft.EnterpriseManagement.Common.TraversalDepth]::Recursive)

foreach($ID in $alreadyinmmformember)

{

$Instance = get-scommonitoringobject -ID $ID

Write-Host "restoring maintenance mode for $($Instance.DisplayName) on $($Instance.Path) to $($minutes) Minutes" -ForegroundColor Yellow

Start-SCOMMaintenanceMode -instance $Instance -endtime $time.addMinutes($minutes)

}


}

Else

{

foreach($ID in $alreadyinmmformember)

{

$Instance = get-scommonitoringobject -ID $ID

Write-Host "restoring maintenance mode for $($Instance.DisplayName) on $($Instance.Path) to $($minutes) Minutes" -ForegroundColor Yellow

Start-SCOMMaintenanceMode -instance $Instance -endtime $time.addMinutes($minutes)

}

}

}

 

This script will first check if the server is in maintenance mode or not. If the server is in maintenance mode, it will remove the server from maintenance mode and restore the maintenance mode for the objects which were previously in maintenance mode in SCOM. Also, if the maintenance mode is already ended by the maintenance schedule, it will restore the maintenance mode for the objects which were previously in maintenance mode in SCOM.

 

Please get the Script from here: https://github.com/souravmahato7/Codes/blob/SCOM/Remove-RestoreMM.ps1

 

Steps to be followed:

 

  1. Firstly we need to keep both the Script in the same location on a management server. (I have kept the Script in C:\Temp\MMScript)

  2. Now we need to create two text file named as AlreadyMM.txt and Servers.txt in the same directory. (I have kept the Script in C:\Temp\MMScript)

  3. Now we need to put the servers information in the Servers.txt file like below

4. Now we need to modify the value of $minutes in the script MMScriptStart.ps1 for how long do you want to put the server in maintenance mode.


5. Now execute the Script MMScriptStart.ps1

A. Open PowerShell with Administrator privilege

B. Go to the same directory where you have kept the Script (I have kept the Script in C:\Temp\MMScript)

 

C. And execute

 

Once the activity completed, please execute the Second script i.e. Remove-RestoreMM.ps1

 

  1. Open PowerShell with Administrator privilege

  2. Go to the same directory where you have kept the Script (I have kept the Script in C:\Temp\MMScript)

 

3. And execute