Ying Li at myITforum.com

PowerShell & System Center

PowerShell script to put the Server, the Health Service and the Health Service Watcher on that server into maintenance mode

In my previous post Schedule a maintenance window I described using PowerShell cmdlet to schedule a maintenance window for target client.

jgoodson made the below comment:

“The script worked great. Can you expand it to set maintenance within Health Service and Health Service Watcher per KB 942866 to cut out Heartbeat Notifications?”

Unfortunately I just noticed this comment today but here is my update:

KB942866

Here is the new script. This is the combined and modified script from my above script and the script from http://scom2k7.blogspot.com/2007/08/new-maintenance-mode-tool-for.html whoever the original author might be.

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

#You only need to do this next line once if you haven’t done so already and this is meant to add the Snapin for SCOM to your regular PowerShell console

Add-PSSnapin "Microsoft.EnterpriseManagement.OperationsManager.Client" -ErrorVariable errSnapin;


Set-Location "OperationsManagerMonitoring::" -ErrorVariable errSnapin;
new-managementGroupConnection -ConnectionString:$rootMS -ErrorVariable errSnapin;
set-location $rootMS -ErrorVariable errSnapin;

$computer  = Get-Agent | Where-object {$_.PrincipalName –eq $computerPrincipalName}
$healthService = $computer.HostedHealthService
$healthServiceWatcherClass = get-monitoringclass -name:Microsoft.SystemCenter.HealthServiceWatcher
$healthServiceCriteria = "HealthServiceName='" + $computerPrincipalName + "'"
$healthServiceWatcher = get-monitoringobject -monitoringclass:$healthServiceWatcherClass -criteria:$healthServiceCriteria
$startTime = [System.DateTime]::Now
$endTime = $startTime.AddHours($numberOfHoursInMaintenanceMode)


"Putting " + $computerPrincipalName + " 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 retuned time is Universal Time

Get-MaintenanceWindow -MonitoringObject $computer.hostcomputer

You can save the above script to your default PowerShell start directory and run the script from there and provice the variables…

Comments

yli628 said:

Let's say if you save the above script as PutServerInmaint.ps1

Here is how you run it!

PS C:\Documents and Settings\yl.admin\My Documents\PS> .\PutServerInMaint.ps1 "xyzopmp01" "abc.whatever.com" 0.25 'test'

Where $RMS = "xyzopm01"

$computerPrincipalName = "abc.whatever.com"

$numberOfHoursInMaintenanceMode = 0.25

$comment = 'test'

# July 10, 2008 5:04 PM

dougwogan said:

What if I would like to run this script from a server other than an OpsManager server.  How do I add the Snapin for SCOM to my PowerShell console?  

Example.  Server1, which has the SCOM agent, is running a maintenance plan at 2am.  I want to add the powershell script as part of the maintenance plan that places server1 in maintenance mode.

# January 9, 2009 4:18 PM

yli628 said:

Run the below script to add SCOM Snapin

Add-PSSnapin "Microsoft.EnterpriseManagement.OperationsManager.Client"

Set-Location "OperationsManagerMonitoring::"

$rootMS = "Whatever"

new-managementGroupConnection -ConnectionString:$rootMS

Set-Location $rootMS

This doesn't have to be run server1 and you cna schedule the task on any machines.

Thanks,

Ying

# January 9, 2009 5:57 PM