Ying Li at myITforum.com

PowerShell & System Center

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

...

Comments

jbeck22 said:

I know this post is realitivly old now, but I'm looking for some help...I copied the above code and I am running it on my SCOM 2007 server.  When I do run the above code I get a ton of errors

>.\FromNet.ps1 c:\list.txt 0.25 'testing'

Get-Agent : The 'Path' parameter is empty or the required provider location is not

set."

At C:\FromNet.ps1:5 char:23

+ $computer  = Get-Agent  <<<< | Where-object {$_.ComputerName -eq $computerName}

Get-MonitoringClass : The 'Path' parameter is empty or the required provider locati

on is not set."

At C:\FromNet.ps1:7 char:49

+ $healthServiceWatcherClass = get-monitoringclass  <<<< -name:Microsoft.SystemCent

er.HealthServiceWatcher

Get-MonitoringObject : Cannot bind argument to parameter 'MonitoringClass' because

it is null.

At C:\FromNet.ps1:9 char:63

+ $healthServiceWatcher = get-monitoringobject -monitoringclass:$ <<<< healthServic

eWatcherClass -criteria:$healthServiceCriteria

Putting c:\list.txt into maintenance mode

New-MaintenanceWindow : Cannot bind argument to parameter 'MonitoringObject' becaus

e it is null.

At C:\FromNet.ps1:14 char:81

+ New-MaintenanceWindow -startTime:$startTime -endTime:$endTime -monitoringObject:$

<<<< computer.HostComputer -comment:$comment

Putting the associated health service into maintenance mode

New-MaintenanceWindow : Cannot bind argument to parameter 'MonitoringObject' becaus

e it is null.

At C:\FromNet.ps1:17 char:81

+ New-MaintenanceWindow -startTime:$startTime -endTime:$endTime -monitoringObject:$

<<<< healthService -comment:$comment

Putting the associated health service watcher into maintenance mode

New-MaintenanceWindow : Cannot bind argument to parameter 'MonitoringObject' becaus

e it is null.

At C:\FromNet.ps1:20 char:81

+ New-MaintenanceWindow -startTime:$startTime -endTime:$endTime -monitoringObject:$

<<<< healthServiceWatcher -comment:$comment

Get-MaintenanceWindow : Cannot bind argument to parameter 'MonitoringObject' becaus

e it is null.

At C:\FromNet.ps1:24 char:40

+ Get-MaintenanceWindow -MonitoringObject  <<<< $computer.hostcomputer

PS C:\

Any ideas on what I did wrong?

Thanks in advance...this is exactly what I was looking for...now if I can just get it to work :)

# November 10, 2009 4:13 PM

yli628 said:

You need to run this in command shell comes with Opsmgr not just regular powershell?

# November 10, 2009 8:20 PM

jbeck22 said:

that's where I was running it from...from my Opsmgr server.

# November 10, 2009 8:40 PM

yli628 said:

Even on the opsmgr server, there are still two powershell console.

# November 10, 2009 8:43 PM

jbeck22 said:

OK...I will double check in the morning when I get back into the office.

# November 10, 2009 10:52 PM

jbeck22 said:

I checked and I am running this code in the Operations Manager Shell...see screenshot here.

i36.photobucket.com/.../OpsMgr.jpg

# November 11, 2009 7:40 AM

yli628 said:

The fact you at the PS c:\ prompt doesn't look right, if you look at my post, you need to be at something like PS Monitoring:\XYZPMP01(replace your server name). Take a look at this post as well, it explains how you add SCOM provider to the regular PS console.

BTW, the Opsmgr R2, you can put computer into maintainence mode by right click.

# November 11, 2009 8:26 PM

jbeck22 said:

So I got the script to work...sort of.

If my list.txt file only has one computer name in it then the script will work just fine, but if I have more than one I get errros.

# November 18, 2009 9:28 AM