SCOrch – Powershell to start Runbook – Part 2   8 comments

I’ve moved everything from this blog over to jmattivi.blogspot.com and updated all of the scripts to have straight quotes.  If any don’t work as posted, please let me know!

For further posts please see jmattivi.blogspot.com.

Thanks!

 

Here is an example to start a Runbook w/ parameters from Powershell.  It’s pretty much the same exact as a standard POST to start a Runbook, but also includes the parameters in the properties struct.

I’ve found that the free program Fiddler helps to find the guids and struct needed to build the POST.  You can also use the Orchestration Console to drill down through to the Instance Summary and Activity Details to find the guids for each parameter.

This is the parameters struct that is added to the regular POST.

Parameter1/Parameter2 are the names that you give the parameter.

You need to specify the guid for each parameter.

Value1/Value2 are the input values you’d like to specify for the Initialize Data activity.

<d:Parameters>&lt;Data&gt;&lt;Parameter&gt;&lt;Name&gt;Parameter1&lt;/Name&gt;&lt;ID&gt;{10000000-0000-0000-0000-000000000000}&lt;/ID&gt;&lt;Value&gt;Value1&lt;/Value&gt;&lt;/Parameter&gt;&lt;Parameter&gt;&lt;Name&gt;Parameter2&lt;/Name&gt;&lt;ID&gt;{20000000-0000-0000-0000-000000000000}&lt;/ID&gt;&lt;Value&gt;Value2&lt;/Value&gt;&lt;/Parameter&gt;&lt;/Data&gt;</d:Parameters>

Here is the complete script to include the parameters in the POST to start the Runbook.  The fields in red would need updated to your environment.

##############################################################################

$creds = Get-Credential(“domain\username”)

$url = “http://scorch.domain:81/Orchestrator2012/Orchestrator.svc/Jobs/

$request = [System.Net.HttpWebRequest]::Create($url)

$request.Credentials = $creds

$request.Timeout = 60000

$request.Accept = “application/atom+xml,application/xml”

$request.Headers.Add(“Accept-Charset”, “UTF-8″)

$request.ContentType = “application/atom+xml”

$request.Method = “POST”

$requestBody = ‘<?xml version=”1.0″ encoding=”utf-8″ standalone=”yes”?>

<entry xmlns:d=”http://schemas.microsoft.com/ado/2007/08/dataservices” xmlns:m=”http://schemas.microsoft.com/ado/2007/08/dataservices/metadata” xmlns=”http://www.w3.org/2005/Atom“>

<content type=”application/xml”>

<m:properties>

<d:Parameters>&lt;Data&gt;&lt;Parameter&gt;&lt;Name&gt;Parameter1&lt;/Name&gt;&lt;ID&gt;{10000000-0000-0000-0000-000000000000}&lt;/ID&gt;&lt;Value&gt;Value1&lt;/Value&gt;&lt;/Parameter&gt;&lt;Parameter&gt;&lt;Name&gt;Parameter2&lt;/Name&gt;&lt;ID&gt;{20000000-0000-0000-0000-000000000000}&lt;/ID&gt;&lt;Value&gt;Value2&lt;/Value&gt;&lt;/Parameter&gt;&lt;/Data&gt;</d:Parameters>

<d:RunbookId type=”Edm.Guid”>00000000-0000-0000-0000-000000000000</d:RunbookId>

</m:properties>

</content>

</entry>

$requeststream=new-object System.IO.StreamWriter $request.GetRequestStream()

$requeststream.Write($requestBody)

$requeststream.Flush()

$requeststream.Close()

$response=$request.GetResponse()

$requestStream=$response.GetResponseStream()

$readStream=new-object System.IO.StreamReader $requestStream

$Output=$readStream.ReadToEnd()

$readStream.Close()

$response.Close()

##############################################################################

8 responses to “SCOrch – Powershell to start Runbook – Part 2

Subscribe to comments with RSS.

  1. jmattivi, Thank you for the great info. I am new to Orchestrator, so I am experimenting with using powershell to start a runbook with parameters. I used your code, but I get a “Exception calling “GetResponse” with “0” argument(s): “The remote server returned an error: (400) Bad Request.” error on the following line.

    $response=$request.GetResponse()

    It appears there is something in the $request it doesn’t like, but I am not sure what it is. I verified that URL is correct. Do you have any ideas of what it could be?

    Thank You

    • Hey Chris,

      First thing that comes to mind could be the copy/paste from WordPress. I just recently found (when I ran into a similar issue) WordPress auto-annoyingly converts straight quotes to smart quotes. I guess the quick fix would be doing a find/replace on the script you paste out updating the single and double quotes. I’ll see if I can find a way to disable them in WordPress and go back through and update previous posts.

      Let me know if that does the trick.

      • Yes, I noticed that too and changed all the quotes. Unfortunately, I still get the error. I found a Microsoft MSDN page (http://msdn.microsoft.com/en-us/library/hh921685.aspx) that shows similar code, but that gives me the same error. I am sure it is something I am missing.

      • Can you start a runbook successfully without parameters? If so, be sure the Parameters tag is updated w/ the parameter name and parameter id unique to your Initiate Data activity. Did you use Fiddler or query the database for the parameter guid(s)? Do the values you’re trying to send contain any greater than or less than characters? SCOrch is veeeery picky when it comes to the formatting/content you can POST to it.

        UPDATED….WordPress also converted the greater than and less than characters….

  2. I wasn’t able to get it working without parameters either. However, I did find the a Powershell module on Codeplex (http://orchestrator.codeplex.com/releases/view/82959) that worked like a champ. I am not sure what I was doing wrong with your code, but I got it working. Thank you for all the help.

    • hmmmm….even from the first post of starting a runbook w/o params, the only things that need updated (in addition to the quotes) are the creds, url/port to the web service server, and the runbook guid.

      Glad you got it working w/ the codeplex scripts though!

  3. I got the same issue as Chris and it was related to the runbook parametersID, which are lower case sensitive.

    Great post
    Simon

  4. I would like to confirm that I had the exact same error as above. Thanks Simon for the tip, the RunbookID and ParameterID’s are stored in the DB as UPPER CASE. The Powershell code REQUIRES THEM TO BE LOWER CASE. Once i changed them to lower case it worked like a charm.

Leave a comment