What if I dont want to put the whole computer into maintenance mode?

Sometimes you may not wish to put the whole computer into maintenance mode. Instead you may wish to put just a number of databases or websites into maintenance mode.

The steps are very simple:

1 - Get the monitoring class that represents the type of monitoring objects that you wish to put into maintenance mode

2 - Get the actual monitoring objects

3 - Iterate through the array of monitoring objects and put them into maintenance mode (dont forget to check whether you got a single monitoring object or an array, this is pretty important) 

Here is a sample of how to do this using PowerShell:

$sql2005DBClass = get-monitoringclass | where {$_.DisplayName -eq 'SQL 2005 DB'}

$dbInstances = get-monitoringobject -monitoringclass:$sql2005DBClass | where {$_.Name -match 'TestDB'}

$startTime = [DateTime]::Now

$endTime = $startTime.AddHours(2)

if($dbInstances -is [Array])
foreach($dbInstance in $dbInstances)
 New-MaintenanceWindow -startTime:$startTime -endTime:$endTime -reason:'PlannedHardwareMaintenance' -monitoringobject:$dbInstance -comment:'comment goes here'
 New-MaintenanceWindow -startTime:$startTime -endTime:$endTime -reason:'PlannedHardwareMaintenance' -monitoringobject:$dbInstances -comment:'comment goes here'

This script will find all databases where the DB name is like TestDB and then put then into maintenance mode for two hours.