~/2010/07/07/schedule-a-url-into-maintenance-mode.md

PowerShell: Schedule a URL into Maintenance Mode

---
author: 
date: 
updated: 
read: 1 min
in:   [engineering, ps, scripts]
tags: [scom, script, powershell]
---

$ grep -n '^#' post.md

Found a nice article about how to script a URL (or web application) into maintenance mode. This was a little bit of a challenge to find so I’m reposting it to hopefully get some more attention. I’ve also posted the modified version of the script that allows for multiple watcher hosts.

Requirements

  • Windows PowerShell 2.0 or later.
  • System Center Operations Manager (SCOM) 2007 R2 console or Command Shell installed, so the Microsoft.EnterpriseManagement.OperationsManager.Client snap-in is available.
  • A SCOM account that is allowed to put monitored objects into maintenance mode.
  • Network access to the root management server.
  • An existing URL (web application) watcher in SCOM, found by its display name.

Parameters

NameTypeRequiredDescription
rootMSStringYesRoot management server used for the management group connection.
urlNameStringYesDisplay name of the URL (web application) watcher, exactly as shown in SCOM. Every object with this name is put into maintenance mode.
minutesIntYesLength of the maintenance window in minutes, starting now.
commentStringNoComment stored on the maintenance window.
reasonStringYesMaintenance mode reason, for example PlannedOther.

Usage

Run the script from a PowerShell session that can see the SCOM snap-in.

powershell
.\URLMaintenanceModeV4.ps1 -rootMS 'scom-rms.example.com' -urlName 'MSN Website Checker' -minutes 45 -comment 'My Comment' -reason 'PlannedOther'

To start it from a scheduled task or a batch file, call powershell.exe explicitly.

powershell
C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe .\URLMaintenanceModeV4.ps1 -rootMS 'scom-rms.example.com' -urlName 'MSN Website Checker' -minutes 45 -comment 'My Comment' -reason 'PlannedOther'

Script

powershell
<#
.SYNOPSIS
    Puts a SCOM URL (web application) watcher into maintenance mode for a number of minutes.
.DESCRIPTION
    Loads the Operations Manager snap-in, connects to the root management server, finds every
    web application watcher whose display name matches urlName and starts a maintenance window
    on each one, beginning now and lasting the given number of minutes.
.PARAMETER rootMS
    Root management server used for the management group connection.
.PARAMETER urlName
    Display name of the URL (web application) watcher, exactly as shown in SCOM. Every object with this name is put into maintenance mode.
.PARAMETER minutes
    Length of the maintenance window in minutes, starting now.
.PARAMETER comment
    Comment stored on the maintenance window.
.PARAMETER reason
    Maintenance mode reason, for example PlannedOther.
.EXAMPLE
    .\URLMaintenanceModeV4.ps1 -rootMS 'scom-rms.example.com' -urlName 'MSN Website Checker' -minutes 45 -comment 'My Comment' -reason 'PlannedOther'
.NOTES
    Author  : Thomas Lasswell (https://www.techcolumnist.com)
    Version : 1.1 (2026-09-20)
    History : 1.0 2010-07-07 Initial version.
              1.1 2026-09-20 Stops with an error when no URL watcher matches the name.
    Requires: SCOM 2007 R2 snap-in Microsoft.EnterpriseManagement.OperationsManager.Client
    Adapted from the original script published at scom2k7.com, modified to loop over multiple watcher hosts.
.LINK
    http://www.scom2k7.com/schedule-a-into-url-maintenance-mode/
#>
param($rootMS, $urlName, $minutes, $comment, $reason)

# Load the snap-in and connect to the management group.
Add-PSSnapin "Microsoft.EnterpriseManagement.OperationsManager.Client" -ErrorVariable errSnapin

Set-Location "OperationsManagerMonitoring::" -ErrorVariable errSnapin
New-ManagementGroupConnection -ConnectionString:$rootMS -ErrorVariable errSnapin
Set-Location $rootMS -ErrorVariable errSnapin

# Find the URL watcher objects by display name.
$URLWatcher = (Get-MonitoringClass -Name Microsoft.SystemCenter.WebApplication.Perspective) | Get-MonitoringObject | Where-Object { $_.DisplayName -eq $urlName }

# Stop if nothing matched, so the script does not report success without doing anything.
if (-not $URLWatcher) {
    Write-Error "No URL watcher found with the display name '$urlName'."
    exit 1
}

# Build the maintenance window.
$startTime = [System.DateTime]::Now
$endTime = $startTime.AddMinutes($minutes)

"Putting URL into maintenance mode"
foreach ($name in $URLWatcher) {
    New-MaintenanceWindow -StartTime:$startTime -EndTime:$endTime -MonitoringObject:$name -Comment:$comment -Reason:$reason
}

Notes

  • Update 2026-09-20 (v1.1): if $urlName matches no URL watcher the script now stops with an error instead of printing "Putting URL into maintenance mode" and doing nothing.

Source

http://www.scom2k7.com/schedule-a-into-url-maintenance-mode/