# encoding: ascii
# api: powershell
# title: Scom2012 Maintenance Mod
# description: Script to put several machines into maintenance mode on your OpsMgr2012 instance.	
# version: 0.1
# type: script
# author: NinjaTechie
# license: CC0
# x-poshcode-id: 4697
# x-archived: 2013-12-16T12:32:27
# x-published: 2013-12-13T13:25:00
#
# Don’t forget to create the Computers.txt file with the servernames in it!
#
#########################################
# MaintenanceMode.ps1
# Created by NinjaTechie 2013-12-12
# http://ninjatechie.net/
# Simple script to place several servers
# into maintenance mode at the same time
#
#########################################
 
$OMServer = "operationsmanagerserver.contoso.local"
$domain = "contoso.local"
$minutes = 240
$computers = '.\Computers.txt'
$class = "Microsoft.Windows.Computer"
$comment = 'Scheduled Maintenance'
 
Import-Module OperationsManager
New-SCOMManagementGroupConnection $OMServer
 
$class = Get-SCOMClass -Name $class
$startTime = [System.DateTime]::Now.ToUniversalTime()
$endTime = [System.DateTime]::Now.AddMinutes($minutes).ToUniversalTime()
 
$ComputerNames = Get-Content $computers
 
foreach ($name in $ComputerNames) {
 $instance = Get-SCOMClassInstance -Class $class | ? { $_.Name -eq ($name + "." + $domain) }
 "Putting " + $name + " into maintenance mode..."
 $instance.ScheduleMaintenanceMode($startTime, $endTime, "PlannedApplicationMaintenance", $comment , "Recursive")
 Start-Sleep -s 2
}
