Share via


Job

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

The Job entity represents a job created by an Orchestrator runbook. The Jobs collection includes all of the jobs run by the local Orchestrator environment.

Properties

The following table lists the properties of the Job entity.

Name Type Key Description

CreatedBy

String

No

CreationTime

DateTime

No

Date and time that the job was created.

Id

GUID

Yes

Unique identifier of the job.

LastModifiedBy

String

No

LastModifiedTime

DataTime

No

Date and time that the job was last modified.

Parameters

String

No

If the job’s runbook requires input parameters, this property will contain XML of the parameters and their values.

RunbookId

GUID

No

Unique identifier of the runbook that started the job.

RunbookServerId

GUID

No

Unique identifier of the runbook server that the job ran on.

RunbookServers

String

No

Status

String

No

Current status of the job.

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 Job Entity Property

Instances

RunbookInstance

Child

JobId

Id

Runbook

Runbook

Parent

Id

RunbookId

RunbookServer

Runbook

Parent

Id

RunbookId

Statistics

Statistic

Child

JobId

Id

Code Samples

Getting Jobs using C#

The following code retrieves all pending and running jobs using C#. It uses a Data Services query in order to step through the different pages of jobs that are returned from the service. For more information, see Paging in Orchestrator Web Service.

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 SingleRunbook
   {
      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<Job> nextJobsLink = null;
         try
         {
            // Setup the query to retrieve the jobs
            var jobsQuery = (from job in context.Jobs
               where (job.Status == "Pending" || job.Status == "Running")
               select job) as DataServiceQuery<Job>;

            // Run the initial query
            var queryResponse = jobsQuery.Execute() as QueryOperationResponse<Job>;
            do
            {
               // Keep querying the next page of jobs until we reach the end
               if (nextJobsLink != null)
               {
                  queryResponse = context.Execute(nextJobsLink) as QueryOperationResponse<Job>;
               }
                    
               // Output the properties of each job on the current page to the console
               foreach (Job job in queryResponse)
               {
                  Console.WriteLine(
                     "Id = {0}\n\tStatus = {1}\n\tCreatedBy = {2}\n\tCreationTime = {3}",
                     job.Id,
                     job.Status,
                     job.CreatedBy,
                     job.CreationTime);
               }
            }
            while ((nextJobsLink = queryResponse.GetContinuation()) != null);
         }
         catch (DataServiceQueryException ex)
         {
            throw new ApplicationException("An error occurred during query execution.", ex);
         }      
      }
   }
}

Getting Jobs using Windows PowerShell

The following code retrieves all pending and running jobs using Windows PowerShell. It pages through the result in case there are more than 50 entries in the result set. The example uses the credentials of the current user context. For more information, see Paging in Orchestrator Web Service. 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.

# Base URL for Jobs collection
$baseUrl = "http://server01.contoso.com:81/Orchestrator2012/Orchestrator.svc/Jobs"

# Assume maximum entries on a page to be 50
$page = 0
$pageSize = 50
do {

    # Setup filter and query options and add to end of base URL for request
    $skip = $page * $pageSize
    $filter = "`$filter=Status eq 'Running' or Status eq 'Pending'"
    $queryOptions = "?$filter&`$skip=$skip&`$top=$pageSize&`$inlinecount=allpages"
    $url = -join ($baseUrl,$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()

    # Setup namespaces for response
    $ns = New-Object Xml.XmlNamespaceManager $output.NameTable
    $ns.AddNamespace( "a", "http://www.w3.org/2005/Atom")
    $ns.AddNamespace( "d", "https://schemas.microsoft.com/ado/2007/08/dataservices")
    $ns.AddNamespace( "m", "https://schemas.microsoft.com/ado/2007/08/dataservices/metadata")

    # Get total number of entries in results so we know how many pages we need to request
    $totalCount = ($output.SelectSingleNode("//m:count",$ns)).InnerText
    $page += 1
    
    # Output properties of each job in page to host
    foreach ($job in $output.feed.entry)
    {
        Write-Host "Id = " $job.content.properties.Id.InnerText 
        Write-Host "Status = " $job.content.properties.Status 
        Write-Host "Created By = " $job.content.properties.CreatedBy 
        Write-Host "Creation Time = " $job.content.properties.CreationTime.InnerText
    }    
} until (($page*$pageSize) -ge $totalCount)  # Continue until all jobs have been displayed

See Also

Concepts

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