OpsMgr 2012: Running a Task in Bulk Using PowerShell

When working with tasks in the Operations Manager 2012 Command Shell, there are four cmdlets you will need to work with:

  • Get-SCOMClass – to retrieve the class of interest
  • Get-SCOMClassInstance  – to retrieve the instances of the class the task will run on
  • Get-Task – to retrieve the task
  • Start-Task – to run the task

I run into some issues when working with the limited examples in the help I wanted to share to demonstrate one path to enabling execution of tasks in bulk – running a task on against multiple instances of a class. In this case, I wanted to work with all instances of “Windows Server 2008 Computer” class and the “List Processes” task. In this post, we’ll cover

  • The Challenge
  • Identifying a More Efficient Way
  • Executing a Task in Bulk

The Challenge

I noticed that when using the examples in the Command Shell help, I could not get straight to the finish, as there were multiple instances of the class.

We start by retrieving the tasks named “List Processes”

There are three of them in this case, targeting different OS classes

image

Next, we’ll retrieve the class instance on which to run the task (a Windows 2008 R2 Server in this case)

Next, we’d run the following to execute the task (as shown in the Command Shell help)

The output, which basically shows that the instance runs all three tasks. The version of the task targeting Windows 2008 Computers actually runs successfully while the other two fail. It works, but it’s just not very efficient. The problem would only be compounded if we were to run the task against multiple instances

image

Identifying a More Efficient Method

To do a little exploring, I started by selecting a single instance of the task and looking at the target to see if I could find a way to easily retrieve only the version of the task I wanted (in this case, the one that targets the Windows 2008 Computer class)

The target didn’t provide much to filter on.

image

I enumerated the properties and methods of the .target to see what is available

image

The identifier in the list above I thought might hold some promise

image

And here I saw the Path, which is what I was looking for.

image

To get the specific task, we just need to filter on the path. The one-liner below returns only the version targeting the Windows Server 2008 Computer class.

image

Now, we simply pass this to start-scomtask and specific the instance of Windows Server 2008 Computer we retrieved at the beginning of the article. (This is a single line configured as two in order to fit on the screen).

Executing the Task in Bulk

Since we know how to retrieve only the version of the task targeting the Windows Server 2008 Computer (Windows.Server.2008.Computer) class, we simply need to retrieve instances of the Windows Server 2008 Computer class,

So the final PowerShell is only two lines of PowerShell

If you look in the “Task Status” view in the Monitoring workspace, you’ll see the output of the “List Processes” task on each Windows Server 2008 Computer in your environment.

Additional  Resources

For more info on the Command Shell in Operations Manager 2012, see these articles:

3 thoughts on “OpsMgr 2012: Running a Task in Bulk Using PowerShell

  1. Pingback: How to deal with SCOM fragmentation alerts? » almoselhy

  2. andyinsdca

    Very groovy. I want to add that if you want to run a task against Agent, you need to specify Microsoft.SystemCenter.Agent for the -name in the GetSSCOM-Class

    And, because I haven’t figured out a better way to do this, here’s how to run a task against a specific agent (server name is mydbserver)

    $instance = get-scomclass -name “Microsoft.SystemCenter.Agent” | Get-SCOMClassInstance

    foreach ($agent in $instance) {

    if ($agent.displayname -eq “mydbserver”) { get-scomdisplaytask “MyTask” | start-scomtask -instance $agent}

    }

     

  3. andyinsdca

    Good stuff. If you want to run tasks for an Agent, the name to use is Microsoft.SystemCenter.Agent. If you want to run a task against a specific agent, here’s how I’m doing it, there’s probably a better way, but I’m not smart enough:

    $instance = get-scomclass -name “Microsoft.SystemCenter.Agent” | Get-SCOMClassInstance

    foreach ($agent in $instance) {

     

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.