PowerShell Script to Put the Agent, the Health Service and the Health Service Watcher on the Agent into Maintenance Mode Revisited
This is an update version of my previous Post
I realize there are may be confusions regarding running OpsMgr related script in regular PowerShell console for some people. This script is for you to directly running from the command shell which comes with OpsMgr console;
I used computerPrincipalName in my previous post, which is FQDN, to save some typing, I use computername instead;
last but not least, I modified the script to work with multiple computers. Enjoy!
*********************************************************************
param($computerNames,$numberOfHoursInMaintenanceMode,$comment)
Foreach ($computerName in $ComputerNames)
{
$computer = Get-Agent | Where-object {$_.ComputerName –eq $computerName}
$healthService = $computer.HostedHealthService
$healthServiceWatcherClass = get-monitoringclass -name:Microsoft.SystemCenter.HealthServiceWatcher
$healthServiceCriteria = "HealthServiceName='" + $computer.PrincipalName + "'"
$healthServiceWatcher = get-monitoringobject -monitoringclass:$healthServiceWatcherClass -criteria:$healthServiceCriteria
$startTime = [System.DateTime]::Now
$endTime = $startTime.AddHours($numberOfHoursInMaintenanceMode)
"Putting " + $computerName + " into maintenance mode"
New-MaintenanceWindow -startTime:$startTime -endTime:$endTime -monitoringObject:$computer.HostComputer -comment:$comment
"Putting the associated health service into maintenance mode"
New-MaintenanceWindow -startTime:$startTime -endTime:$endTime -monitoringObject:$healthService -comment:$comment
"Putting the associated health service watcher into maintenance mode"
New-MaintenanceWindow -startTime:$startTime -endTime:$endTime -monitoringObject:$healthServiceWatcher -comment:$comment
#To confirm your result and the returned time is Universal Time
Get-MaintenanceWindow -MonitoringObject $computer.hostcomputer
}
*************************************************************************
Save the above script (Between the lines) as whatever.ps1
Start your command shell with your admin (for OpsMgr) account and type the following in the prompt
PS Monitoring:\XYZPMP01
>c:\MyWorkSpace\Whatever.ps1 (gc c:\MyWorkSpace\list.txt) 0.25 'UpdateXXX'
Your list.txt should look like this:
server1
server2
...