The Operations Manager class libraries allow you to control the scheduling of maintenance windows. A maintenance window is the period of time during which a monitored object is placed in maintenance mode and, consequently, does not generate alerts. Scheduling maintenance windows is useful when you need to place a large number of monitored objects in maintenance mode at one time (for example, to apply updates to the monitored objects) or when you need to run a scheduled task to automatically control the maintenance window.

Example

Example 1—Starting Maintenance Mode

The following example demonstrates how to place the monitoring objects for all instances of SQL Server 2005 Database Engine in maintenance mode for an hour.

Visual Basic  Copy Code
' This example places all instances of SQL Server 2005 Database
' Engines in maintenance mode for one hour.
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports Microsoft.EnterpriseManagement
Imports Microsoft.EnterpriseManagement.Configuration
Imports Microsoft.EnterpriseManagement.Administration
Imports Microsoft.EnterpriseManagement.Common
Imports Microsoft.EnterpriseManagement.Monitoring
Imports System.Collections.ObjectModel

Namespace SDKSamples
	Class Program
		Public Overloads Shared Function Main(ByVal args() As String) As Integer

			Dim mg As ManagementGroup = New ManagementGroup("localhost")

			Console.WriteLine("Creating a maintenance window...")

			Dim query As String = "DisplayName = 'SQL 2005 DB Engine'"
			Dim criteria As MonitoringClassCriteria = New MonitoringClassCriteria(query)
			Dim monClasses As ReadOnlyCollection(Of MonitoringClass) = _
				mg.GetMonitoringClasses(criteria)
			Dim monObjects As List(Of MonitoringObject) = New List(Of MonitoringObject)()
			For Each monClass As MonitoringClass In monClasses

				monObjects.AddRange(mg.GetMonitoringObjects(monClass))
			Next

			For Each monObject As MonitoringObject In monObjects

				If Not monObject.InMaintenanceMode Then

					Dim startTime As DateTime = DateTime.UtcNow
					Dim schedEndTime As DateTime = DateTime.UtcNow.AddHours(1)
					Dim reason As MaintenanceModeReason = MaintenanceModeReason.SecurityIssue
					Dim comment As String = "Need to apply the latest update to all SQL 2005 servers."
					monObject.ScheduleMaintenanceMode(startTime, schedEndTime, reason, comment)
					Console.WriteLine(monObject.DisplayName & " set in maintenance mode for an hour.")

				Else

					Dim window As MaintenanceWindow = monObject.GetMaintenanceWindow()
					Dim schedEndTime As DateTime = window.ScheduledEndTime
					Console.WriteLine(monObject.DisplayName & " already in maintenance mode until " & _
									schedEndTime.ToShortTimeString() & ".")
				End If
			Next
		End Function
	End Class
End Namespace
C#  Copy Code
/// <summary>
/// This example places all instances of SQL Server 2005 Database
/// Engines in maintenance mode for one hour.
/// </summary>
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Microsoft.EnterpriseManagement;
using Microsoft.EnterpriseManagement.Administration;
using Microsoft.EnterpriseManagement.Common;
using Microsoft.EnterpriseManagement.Configuration;
using Microsoft.EnterpriseManagement.Monitoring;
using System.Text;

namespace SDKSamples
{
	class Program
	{
		static void Main(string[] args)
		{
			ManagementGroup mg = new ManagementGroup("localhost");

			Console.WriteLine("Creating a maintenance window...");

			string query = "DisplayName = 'SQL 2005 DB Engine'";
			MonitoringClassCriteria criteria = new MonitoringClassCriteria(query);
			ReadOnlyCollection<MonitoringClass> monClasses = 
				mg.GetMonitoringClasses(criteria);
			List<MonitoringObject> monObjects = new List<MonitoringObject>();
			foreach (MonitoringClass monClass in monClasses)
			{
				monObjects.AddRange(mg.GetMonitoringObjects(monClass));
		}

			foreach (MonitoringObject monObject in monObjects)
			{
				if (!monObject.InMaintenanceMode)
				{
					DateTime startTime = DateTime.UtcNow;
					DateTime schedEndTime = DateTime.UtcNow.AddHours(1);
					MaintenanceModeReason reason = MaintenanceModeReason.SecurityIssue;
					String comment = "Need to apply the latest update to all SQL 2005 servers.";
					monObject.ScheduleMaintenanceMode(startTime, schedEndTime, reason, comment);
					Console.WriteLine(monObject.DisplayName + " set in maintenance mode for an hour.");
			}
				else
				{
					MaintenanceWindow window = monObject.GetMaintenanceWindow();
					DateTime schedEndTime = window.ScheduledEndTime;
					Console.WriteLine(monObject.DisplayName + " already in maintenance mode until " + schedEndTime.ToShortTimeString() + ".");
			}
		}
	}
}
}

Example 2—Ending Maintenance Mode

  • The following example demonstrates how to immediately end the maintenance mode of monitoring objects for all instances of SQL Server 2005 Database Engine.
Visual Basic  Copy Code
' <summary> 
' Immediately takes all instances of SQL Server 2005 Database
' Engines out of maintenance mode.
' </summary>
Imports System
Imports System.Collections.Generic
Imports System.Collections.ObjectModel
Imports System.Text
Imports Microsoft.EnterpriseManagement
Imports Microsoft.EnterpriseManagement.Administration
Imports Microsoft.EnterpriseManagement.Configuration
Imports Microsoft.EnterpriseManagement.Common
Imports Microsoft.EnterpriseManagement.Monitoring


Namespace SDKSamples
	Class Program
		Public Overloads Shared Function Main(ByVal args() As String) As Integer

			Dim mg As ManagementGroup = New ManagementGroup("localhost")

			Console.WriteLine("Stopping maintenance mode...")

			Dim query As String = "DisplayName = 'SQL 2005 DB Engine'"
			Dim criteria As MonitoringClassCriteria = New MonitoringClassCriteria(query)
			Dim monClasses As ReadOnlyCollection(Of MonitoringClass) = _
				mg.GetMonitoringClasses(criteria)
			Dim monObjects As List(Of MonitoringObject) = New List(Of MonitoringObject)()
			For Each monClass As MonitoringClass In monClasses

				monObjects.AddRange(mg.GetMonitoringObjects(monClass))
			Next

			For Each monObject As MonitoringObject In monObjects

				If (monObject.InMaintenanceMode) Then

					monObject.StopMaintenanceMode(DateTime.UtcNow)
					Console.WriteLine(monObject.DisplayName & " set out of maintenance mode.")

				Else

					Console.WriteLine(monObject.DisplayName & " not in maintenance mode.")
				End If
			Next
		End Function
	End Class
End Namespace
C#  Copy Code
/// <summary>
/// Immediately takes all instances of SQL Server 2005 Database
/// Engines out of maintenance mode.
/// </summary>
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Microsoft.EnterpriseManagement;
using Microsoft.EnterpriseManagement.Administration;
using Microsoft.EnterpriseManagement.Common;
using Microsoft.EnterpriseManagement.Configuration;
using Microsoft.EnterpriseManagement.Monitoring;
using System.Text;

namespace SDKSamples
{
	class Program
	{
		static void Main(string[] args)
		{
			ManagementGroup mg = new ManagementGroup("localhost");

			Console.WriteLine("Stopping maintenance mode...");

			string query = "DisplayName = 'SQL 2005 DB Engine'";
			MonitoringClassCriteria criteria = new MonitoringClassCriteria(query);
			ReadOnlyCollection<MonitoringClass> monClasses =
				mg.GetMonitoringClasses(criteria);
			List<MonitoringObject> monObjects = new List<MonitoringObject>();
			foreach (MonitoringClass monClass in monClasses)
			{
				monObjects.AddRange(mg.GetMonitoringObjects(monClass));
		}

			foreach (MonitoringObject monObject in monObjects)
			{
				if (monObject.InMaintenanceMode)
				{
					monObject.StopMaintenanceMode(DateTime.UtcNow);
					Console.WriteLine(monObject.DisplayName + " set out of maintenance mode.");
			}
				else
				{
					Console.WriteLine(monObject.DisplayName + " not in maintenance mode.");
			}
		}
	}
}
}

See Also


Send comments about this topic to Microsoft.
[an error occurred while processing this directive]