Running Tasks

You can run a task from the Command Shell much like you would from the UI Console. You start by selecting the object you want to work with then you select the task you want to run. In this scenario we are going to look at resetting the Health Service store of a hypothetical agent named echo12.contoso.com.

 

Like most blog posts all of the examples are based on working directly from the Root Management Server with a connection defined to localhost. If that is not how you are working simply replace 'localhost' with the name of your connection in the examples.

Monitoring:\localhost

>

 

Let's start by moving to the location of the Health Service of the computer we want to manage. If you are not sure just list all of the items under Microsoft.SystemCenter.AllComputersGroup by using 'dir' or the 'Get-ChildItem' command

# Move to the AllComputersGroup.

cd Microsoft.SystemCenter.AllComputersGroup

# List all of the member names of the AllComputersGroup

dir | Format-List PathName

# Move to the location of one of the members of the All Computers Group.

cd echo12.contoso.com

# Move to the location of the Health Service running on your agent.

cd Microsoft.SystemCenter.HealthService

 

Your prompt should look similar to the following. Much like all of the other Monitoring Object centric Cmdlets both Get-Task and Start-Task use the path as a context from which to work.

Monitoring:\localhost\Microsoft.SystemCenter.AllComputersGroup\echo12.contoso.com\Microsoft.SystemCenter.HealthService

>

Now let's get all the tasks for the current Health Service instance.

Get-Task | Format-List Name

We only care about resetting the Health Service store. We need to filter the results so that only the 'ResetHealthServiceStore' task is returned.

Get-Task | where {$_.Name -eq "Microsoft.SystemCenter.ResetHealthServiceStore"}

Once you have verified that your filter is working you can start the task.

Get-Task | where {$_.Name -eq "Microsoft.SystemCenter.ResetHealthServiceStore"} | Start-Task

Optionally you could create a variable to hold the task you want to start.

$task = Get-Task | where {$_.Name -eq "Microsoft.SystemCenter.ResetHealthServiceStore"}

Start-Task -Task: $task

The Start-Task Cmdlet will block until the task it is complete. If that is not the desired behavior specify the Asynchronous switch.

Start-Task -Task: $task -Asynchronous

If you want to see the status of your task a later time you can store the Task Result and use it to query for status updates.

$taskResult = Start-Task -Task: $task -Asynchronous

Get-TaskResult $taskResult.Id

If the task requires credentials you must first enter your credentials and store them in a variable.

$creds = Get-Credential

Start-Task -Task: $task -Credential: $creds

 

Last but not least you can specify overrides for existing properties of a task. I recommend specifying overrides by defining a variable so that you can verify your work before submitting the task. The following example is fictitious and is only used to illustrate how you would define overrides for a task.

$overrides = @{SignatureID=10; AnotherID="1092834098123407953912837"}

Start-Task -Task: $task -Override: $overrides

If you are not sure which overrides are available for a given task you can use the Get-Overrides Cmdlet. The following example lists the overrides for the task stored in the $task variable.

$task | Get-Override 

Hope that helps and as always please post your questions and comments and I will make sure they are addressed in future posts.

Roger Sprague