Putting a Group of Computers into Maintenance Mode via Windows PowerShell

Putting a Group of Computers into Maintenance Mode via Windows PowerShell

Have you ever found yourself in need of putting a group of systems into Maintenance Mode in System Center Operations Manager 2007 R2?  If so you can use the script below.  Note that in SCOM 2007 R2 there is no need to put the health service and health service watcher into Maintenance Mode separately, we just need to put the computer itself into Maintenance Mode and it will take care of other components automatically.

*************Script*********

param($rootMS,$group,$numberOfHoursInMaintenanceMode,$comment)

Add-PSSnapin "Microsoft.EnterpriseManagement.OperationsManager.Client" -ErrorVariable errSnapin;
Set-Location "OperationsManagerMonitoring::" -ErrorVariable errSnapin;
new-managementGroupConnection -ConnectionString:$rootMS -ErrorVariable errSnapin;
set-location $rootMS -ErrorVariable errSnapin;

$groupObject = get-monitoringobject | where {$_.DisplayName -eq $group};
$groupagents = $groupObject.getrelatedmonitoringobjects()

foreach ($agent in $groupAgents)

{

$computerPrincipalName = $agent.displayname
$computerPrincipalName

$computerClass = get-monitoringclass -name:Microsoft.Windows.Computer
$healthServiceClass = get-monitoringclass -name:Microsoft.SystemCenter.HealthService
$healthServiceWatcherClass = get-monitoringclass -name:Microsoft.SystemCenter.HealthServiceWatcher

$computerCriteria = "PrincipalName='" + $computerPrincipalName + "'"
$computer = get-monitoringobject -monitoringclass:$computerClass | Where{$_.Displayname -like $computerPrincipalName}
$startTime = [System.DateTime]::Now
$endTime = $startTime.AddHours($numberOfHoursInMaintenanceMode)

"Putting " + $computerPrincipalName + " into maintenance mode"
New-MaintenanceWindow -startTime:$startTime -endTime:$endTime -monitoringObject:$computer -comment:$comment

}

*************************

Save the above script with a ps1 extension.

Schedule the script using the Task Scheduler and command line below:

Powershell c:\MMode.ps1 -rootMS: <RMS server name> -group: <Group name> - Numberofhoursinmaintenancemode: 1 -Comment: 'This is a test'

Note: This information was originally contributed by Ritesh Grover, System Center Technical Lead, on the Operations Manager Support Team blog:

http://blogs.technet.com/operationsmgr/archive/2009/11/17/putting-a-group-of-computers-into-maintenance-mode-via-powershell.aspx

Leave a Comment
  • Please add 7 and 4 and type the answer here:
  • Post
Wiki - Revision Comment List(Revision Comment)
Sort by: Published Date | Most Recent | Most Useful
Comments
  • Richard Mueller edited Revision 3. Comment: Removed (en-US) from title

  • Craig Lussier edited Revision 1. Comment: added en-US to tags and title

Page 1 of 1 (2 items)
Wikis - Comment List
Sort by: Published Date | Most Recent | Most Useful
Posting comments is temporarily disabled until 10:00am PST on Saturday, December 14th. Thank you for your patience.
Comments
  • Craig Lussier edited Revision 1. Comment: added en-US to tags and title

  • Richard Mueller edited Revision 3. Comment: Removed (en-US) from title

Page 1 of 1 (2 items)