Share via


Activity Instance

Applies To: System Center 2012 - Orchestrator, System Center 2012 R2 Orchestrator, System Center 2012 SP1 - Orchestrator

The Activity Instance entity represents an activity instance that is part of an Orchestrator runbook instance. The Activity Instances collection includes all of the activity instances in the local Orchestrator environment. You can use the RunbookInstance relationship to retrieve the activity instances that are part of a particular runbook instance.

Properties

The following table lists the properties of the ActivityInstance entity.

Name Type Key Description

ActivityInstance

GUID

No

Unique identifier of the activity that the activity instance came from.

EndTime

DateTime

No

The date and time that the activity instance ended.

Id

GUID

Yes

Unique identifier of the job.

RunbookInstanceId

GUID

No

Unique identifier of the runbook instance that the activity instance came from.

SequenceNumber

Integer

No

Number indicating the relative order that the activity ran in the runbook instance.

Status

String

No

Completion status of the activity instance.

StartTime

DateTime

No

The date and time that the activity instance started.

Relationships

The following table lists the entities that share a relationship with the Job entity and the key property in each entity used to define the relationship.

Collection Entity Relationship Related Entity Property ActivityInstance Entity Property

Activity

Activity

Parent

Id

Activity Id

Data

ActivityInstanceData

Child

ActivityInstanceId

Id

RunbookInstance

RunbookInstance

Parent

Id

RunbookInstanceId

Code Samples

Getting Activity Instances using C#

The following code retrieves all activity instances from a particular runbook instance using C#. This example uses a service reference named SCOService and uses the credentials of the current user context. You can uncomment the line that specifies alternate credentials if the current user does not have appropriate permissions to the runbook being started. For more information see Programming in Visual Studio With the Orchestrator Web Service and Authentication and Authorization.

namespace CodeSample.Microsoft.SystemCenter.Orchestration.WebService
{
   using System;
   using System.Collections;
   using System.Collections.Generic;
   using System.Linq;
   using System.Text;
   using System.Net;
   using System.IO;
   using System.Data.Services.Client;
   using SCO.SCOService;   

   public class ActivityInstances
   {
      public static void Main()
      {
         Guid runbookId = new Guid("00000000-0000-0000-0000-000000000000");

         // Path to Orchestrator web service
         string serviceRoot = "http://server01.contoso.com:81/Orchestrator2012/Orchestrator.svc";

         // Create Orchestrator context
         SCOService.OrchestratorContext context = new SCOData.OrchestratorContext(new Uri(serviceRoot));

         // Set credentials to default or to a specific user.
         context.Credentials = System.Net.CredentialCache.DefaultCredentials;
         //context.Credentials = new System.Net.NetworkCredential("user", "pwd", "domain");

         // Setup Data Services query
         DataServiceQueryContinuation<RunbookInstance> nextRunbookInstanceLink = null;
         try
         {
            // Setup the query to retrieve the runbook instance. In this case, it’s the last instance to have run.
            RunbookInstance runbookInstance = (from rbkInstance in context.RunbookInstances
                                         where rbkInstance.RunbookId == runbookId
                                         orderby rbkInstance.CreationTime descending
                                         select rbkInstance).FirstOrDefault();

            // Query for the activity instances of the runbook instance
            IEnumerable<RunbookInstanceParameter> runbookInstanceParameters = context.RunbookInstanceParameters.Where(rip => rip.RunbookInstanceId == runbookInstance.Id);

            // Output the properties of each instance to the console
            foreach (RunbookInstanceParameter runbookInstanceParameter in runbookInstanceParameters)
            {
               Console.WriteLine("Direction: {0}, Name: {1}, Value: {2}", 
                                  runbookInstanceParameter.Direction, 
                                  runbookInstanceParameter.Name, 
                                  runbookInstanceParameter.Value);
            }
         }
         catch (DataServiceQueryException ex)
         {
            throw new ApplicationException("An error occurred during query execution.", ex);
         }      
      }
   }
}

Getting Activity Instances using Windows PowerShell

The following code retrieves all activity instances from a particular runbook instance using Windows PowerShell. The example uses the credentials of the current user context. You can uncomment the line that specifies alternate credentials if the current user does not have appropriate permissions to the runbook being started. For more information see Authentication and Authorization.

# Details of the runbook we want to get
$runbookId = "6e432b61-a838-49cf-a073-924f58b63f73"

# Create the request object for the runbook instance
$runbookInstanceUrl = -join ("http://server01.contoso.com:81/Orchestrator2012/Orchestrator.svc/","Runbooks(guid'",$runbookId.ToString(),"')/Instances")
$queryOptions = "?`$orderby=CreationTime desc&`$top=1"
$url = -join($runbookInstanceUrl,$queryOptions)
$request = [System.Net.HttpWebRequest]::Create($url)

# Set the credentials to default or prompt for credentials
$request.UseDefaultCredentials = $true
# $request.Credentials = Get-Credential

# Build the request header
$request.Method = "GET"
$request.UserAgent = "Microsoft ADO.NET Data Services"

# Get the response from the request
[System.Net.HttpWebResponse] $response = [System.Net.HttpWebResponse] $request.GetResponse()

# Write the HttpWebResponse to String
$reader = [IO.StreamReader] $response.GetResponseStream()  
$output = $reader.ReadToEnd()  
[xml]$output = $output
$reader.Close()

# Get the runbook instance Id which we need for the next request
$runbookInstanceId = $output.feed.entry.content.properties.id.innertext

# Create the request object for the parameters
$activityUrl = -join ("http://server01.contoso.com:81/Orchestrator2012/Orchestrator.svc/","RunbookInstances(guid'",$runbookInstanceId.ToString(),"')/ActivityInstances")
$request = [System.Net.HttpWebRequest]::Create($activityUrl)

# Set the credentials to default or prompt for credentials
$request.UseDefaultCredentials = $true
# $request.Credentials = Get-Credential

# Build the request header
$request.Method = "GET"
$request.UserAgent = "Microsoft ADO.NET Data Services"

# Get the response from the request
[System.Net.HttpWebResponse] $response = [System.Net.HttpWebResponse] $request.GetResponse()

# Write the HttpWebResponse to String
$reader = [IO.StreamReader] $response.GetResponseStream()  
$output = $reader.ReadToEnd()  
[xml]$output = $output
$reader.Close()

# Output properties of each parameter
foreach ($activityInstance in $output.feed.entry)
{
    Write-Host "Id: " $activityInstance.content.properties.Id.InnerText
    Write-Host "Sequence: " $activityInstance.content.properties.SequenceNumber.InnerText
    Write-Host "Start Time: " $activityInstance.content.properties.StartTime.InnerText
    Write-Host "Status: " $activityInstance.content.properties.Status
}

See Also

Concepts

Programming Using the Orchestrator Web Service
OData Queries Using the Orchestrator Web Service
Authentication and Authorization