End SCOM maintenance mode on multiple servers

SCOM

Today I would like to share with you a one of SCOM scripts which I use on regular basis. Some of you are working in large scale environment and have to put large amount of servers in SCOM maintenance mode. In this article you will find out how to stop it for some specific machines.

End SCOM maintenance mode

In one of the previous articles I was describing how to put servers in SCOM maintenance mode. This time I want to show you how to end it.

To end it for a single machine we just have to specify SCOM class and then get class instances. Next we have to use StopMaintenaceMode method:

$CompClass = Get-SCOMClass -Name Microsoft.Windows.Computer
$WindowsComputer = Get-SCOMClassInstance -class $CompClass
$Server = "DC01"
($WindowsComputer | Where-Object {$_.displayname –match $server}).StopMaintenanceMode([DateTime]::Now.ToUniversalTime(),[Microsoft.EnterpriseManagement.Common.TraversalDepth]::Recursive)

Final script:

            Write-Host "Removing SCOM Maintenance Mode" -ForegroundColor Green
            $Servers = $null 
            "Selecting servers list"
            #Browsing file
            Add-Type -AssemblyName System.Windows.Forms
            $FileBrowser = New-Object System.Windows.Forms.OpenFileDialog
            $FileBrowser.filter = "Txt (*.txt)| *.txt"
            $FileBrowser.InitialDirectory = "c:\users\$env:username\desktop"
            [void]$FileBrowser.ShowDialog()
  
            #Getting servers from txt file
            Try{
                $FilePath = $FileBrowser.FileName
                $Servers = Get-Content -Path $FilePath -ErrorAction Stop

                "Importing SCOM module"
                Import-Module OperationsManager
            }
            Catch{
              $_.Exception.Message
              Break
            }

            If( $Servers.length -eq 0 ){ 
                Write-Warning "Servers list is empty"
                Pause 
                Break
            }

            "Getting SCOM class"
            $CompClass = Get-SCOMClass -Name Microsoft.Windows.Computer

            "Removing servers from SCOM MM - please wait"
            $WindowsComputer = Get-SCOMClassInstance -class $CompClass
            $ErrorActionPreference = "Stop"
            Foreach ($Server in $Servers) {
                $Server = $Server.trim()
                "Processing $Server"
                Try{
                    ($WindowsComputer | Where-Object {$_.displayname –match $server}).StopMaintenanceMode([DateTime]::Now.ToUniversalTime(),[Microsoft.EnterpriseManagement.Common.TraversalDepth]::Recursive);
                }
                Catch{
                    $_.Exception.Message
                    Continue
                }
            }
            Pause

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.