Just Another Maintenance Mode Script for SCOM 2007

 

This is based on Timoty Mc Fadden’s Remote Maintenance Mode scheduler:

http://www.scom2k7.com/scom-remote-maintenance-mode-scheduler-20/

for which many thanks!

I fiddled a little with the script, so it can be used to set a SCOM Group in Maintenance Mode, containing different kinds (types) of Objects.

I needed to do this for a customer, who performed regular maintenance on related

servers, but it wasn’t always necessary to put the entire computer in Maintenance Mode. It should be easy to add other classes as needed.

Here’s the code:

Param($rootMS,$groupName,$minutes,$comment,$reason,$startMM)

# Load Ops Mgr Snap-In
Add-PsSnapin “Microsoft.EnterpriseManagement.OperationsManager.Client”
# Go to Monitoring Drive
Set-Location “OperationsManagerMonitoring::”

# Connect to Management Group
$MGConn = New-ManagementGroupConnection -connectionString:$rootMS

If (!($MGConn))
{
Write-Host “Failed to connect to RMS”
Exit
}

# Go to RMS location on Monitoring Drive
Set-Location $rootMS;

# Instantiate objects for necessary classes
$GroupClass = Get-MonitoringClass -Name:System.Group
$windowsComputerClass = Get-MonitoringClass -Name:Microsoft.Windows.Computer
$IISServerRoleClass = Get-MonitoringClass -Name:Microsoft.Windows.InternetInformationServices.ServerRole
$WindowsClusterClass = Get-MonitoringClass -Name:Microsoft.Windows.Cluster

# Instantiate Group that was given as parameter
$GroupInstance = (Get-ManagementGroupConnection).ManagementGroup.GetPartialMonitoringObjects($GroupClass) | where {$_.DisplayName -eq $groupName}

if (!($GroupInstance))
{
 Write-Host “Specified Group not found”
 Exit
}
# Function that activates the Maintenance Mode
Function PutInMaintMode ($Objects) {

ForEach($Object in $Objects)
 {
Write-Host “Object Fullname: ” + $Object.FullName
$startTime = [DateTime]::Now
$endTime = $startTime.AddMinutes($minutes)

if($startMM -eq $true -and $Object.InMaintenanceMode -eq $false)
  {
   New-MaintenanceWindow -startTime:$startTime -endTime:$endTime -comment:$comment -Reason:$reason -monitoringObject:$Object
 }   
}
 }

# Retrieve all objects contained in the group.
# You can add classes here if necessary
$Computers = $groupInstance.GetRelatedMonitoringObjects($windowsComputerClass,[Microsoft.EnterpriseManagement.Common.TraversalDepth]::OneLevel)
$IISServers = $groupInstance.GetRelatedMonitoringObjects($IISServerRoleClass,[Microsoft.EnterpriseManagement.Common.TraversalDepth]::OneLevel)
$WindowsClusters = $groupInstance.GetRelatedMonitoringObjects($WindowsClusterClass,[Microsoft.EnterpriseManagement.Common.TraversalDepth]::OneLevel)

# Call the Maintenance Mode Function per Class
PutInMaintMode $Computers
PutInMaintMode $IISServers
PutInMaintMode $WindowsClusters

Advertisements

One response to “Just Another Maintenance Mode Script for SCOM 2007

  1. My brother recommended I may like this website.

    He used to be entirely right. This post actually made my day.

    You can not imagine just how so much time I had spent for this info!
    Thank you!

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s