Stop a Job

System Center

Updated: December 20, 2012

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

To stop a runbook using the Orchestrator web service, you stop its current job. You stop a job by setting its status to "Canceled”.

Stopping a job with C#

The following example shows how to stop a job for a specific runbook 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.SystemCenter2012.Orchestrator.WebService
   using System;
   using System.Collections.Generic;
   using System.Linq;
   using System.Text;
   using System.Net;
   using System.IO;
   using System.Collections;
   using System.Data.Services.Client;
   using SCOService;
   public class RunbookOperations 
      public void StopJob()
         // Details of job to stop
         Guid jobId = new Guid("00000000-0000-0000-0000-000000000000");

         // Path to Orchestrator web service
         string serviceRoot = "http:/";

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

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

             // Get the job
             var job = context.Jobs.Where(j => j.Id == jobId).SingleOrDefault();

             // Set the job status and save the change
             if (job != null)
                 job.Status = "Canceled";
         catch (DataServiceQueryException ex)
             throw new ApplicationException("An error occurred during cancel job execution.", ex);

Stopping a job with Windows PowerShell

Windows PowerShell does not have the ability to use a Service Reference as does a program written using Visual Studio. Instead, you must create an http request that gets the details of the job that you want to stop. You then modify the response from that request and use it to create a POST request that actually stops it. The following example shows how to stop a job using Windows PowerShell.

# Details of the job we are going to stop
$jobid = "00000000-0000-0000-0000-000000000000"  

$joburl = -join ("'",$jobid,"')")

# Get the current job
$jobrequest = [System.Net.HttpWebRequest]::Create($joburl)
$jobrequest.Method = "GET"
$jobrequest.UserAgent = "Microsoft ADO.NET Data Services"

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

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

# Build the XML for the job that we will use in the POST request
$reader = [IO.StreamReader] $jobresponse.GetResponseStream()  
$jobxml = $reader.ReadToEnd()  
[xml]$jobxml = $jobxml

# Modify date/time properties for the job and specify that it should be stopped 
$lastmodifiedtime = $jobxml.DocumentElement.Item("content").Item("m:properties").Item("d:LastModifiedTime").InnerText
$creationtime = $jobxml.DocumentElement.Item("content").Item("m:properties").Item("d:CreationTime").InnerText
$jobxml.DocumentElement.Item("published").InnerText = $creationtime + "Z"
$jobxml.DocumentElement.Item("updated").InnerText = $lastmodifiedtime + "Z"
$jobxml.DocumentElement.Item("content").Item("m:properties").Item("d:Status").InnerText = "Canceled"

# Remove the <link> nodes from the job XML
$linknode = $jobxml.DocumentElement.Item("link")
while ($linknode -ne $null)
    $linknode = $jobxml.DocumentElement.Item("link")

# Use the new job XML for the request body of the post request
$postrequestbody = $jobxml.get_outerxml()

# Setup the post request
$postrequest = [System.Net.HttpWebRequest]::Create($joburl)

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

# Build the request header
$postrequest.Method = "POST"
$postrequest.UserAgent = "Microsoft ADO.NET Data Services"
$postrequest.Accept = "application/atom+xml,application/xml"
$postrequest.ContentType = "application/atom+xml"
$postrequest.KeepAlive = $true

# Add header properties specific for Cancel Job
$lmtime = $lastmodifiedtime.Replace(":","%3A")
$tempstring = -join ("W/" , '"' , "datetime'" , $lmtime , "'" , '"')

# Create a request stream from the request
$requestStream = new-object System.IO.StreamWriter $postrequest.GetRequestStream()
# Sends the request to the service
[System.Net.HttpWebResponse] $response = [System.Net.HttpWebResponse] $postrequest.GetResponse()

See Also