Saturday, June 2, 2012

SCOrch – Powershell to start Runbook – Part 2

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 highlighted fields 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()

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

9 comments:

  1. Hi Jon,

    Why should I myself "translate" the parameter name to a GUID? Is PowerShell not able to retrieve the GUID by the parameter's name?
    This would be an improvement of your script to trigger the runbook by parameter name and value only!
    Please rethink your approach.

    Many thanks,

    Michael

    ReplyDelete
  2. You could accomplish the translation through multiple web service calls or a database query I've posted here - http://jmattivi.blogspot.com/2012/07/find-runbookparameter-guids-for-web.html.

    This and the previous post are the examples of the building blocks of what is required to invoke a runbook through the web service. I have a few ideas of using powershell to pull the guids out of the database based on script parameters....If/when I get time I'll do a write up on it....in the meantime, feel free to improve upon the code and add inputs to the script.

    ReplyDelete
  3. Thanks for the post. It's a fine start. I think getting the GUIDs from the SCO DB with SQL Management Studio is pretty straight forward. And, I like to have the GUIDs anyway. It keeps from launching the wrong runbook. You have to set the GUIDs once per script. Not that tough.

    ReplyDelete
  4. When I use "$request.UseDefaultCredentials = $true" instead of "$request.Credentials = $creds" I get your script to work, but when I use "$request.Credentials = $creds" I get
    Exception calling "GetResponse" with "0" argument(s): "The remote server returned an error: (401) Unauthorized." Is ther somethin I need to configure in IIS? sthom@rim.com

    ReplyDelete
    Replies
    1. Hi,

      What value are you setting "$creds" to? $creds should be set with a user or service account that has permissions applied through the folder structure in the designer console. The error you're receiving is due to invalid credentials authenticating to send the POST request.

      Jon

      Delete
  5. Hello Jon,

    I've been trying to get this to work, and apparently I'm doing something wrong. No matter what I try, i get the following error when I run the script.

    Exception calling "GetResponse" with "0" argument(s): "The remote server returned an error: (500) Internal Server Error."

    I turned on failed request logging, and to some degree has been helpful, well until I got to this point. Now basically the only error I'm seeing the failed request log is the following:





    An error occurred while processing this request.



    Any suggestions, or pointers you can provide would be greatly appreciated.

    ReplyDelete
  6. Also, while I don't think I screwed up the syntax of the parameters, anything is possible, so I threw a copy of your script, with my edits (sanitized) up on pastebin, in case you'd like to have a look at it.

    http://pastebin.com/F805Xa5E

    thanks again.



    ReplyDelete
    Replies
    1. Hello,

      I took a peek at the code you posted and nothing is jumping out initially....one thing that comes to mind........is the runbook guid all lower case?

      Example:
      8bcde055-629c-4c30-113f-e0f131d22erf

      Are you using SSL for the Orchestration Console and web service?

      Rather than a POST call....can you test a GET just to determine that the web service is working correctly and then build up to the POST? Even start a runbook through a POST without params and then once that's working add params back in.

      Let me know,
      Jon

      Delete
    2. Hello Jon,

      Yeah, the lower case GUID thing I discovered via your old blog site, thanks to the comments. That was my first hurdle.

      Both the console and the web service are using SSL, and I get the same response hitting either the http or https.

      Running a GET seems to work fine.

      I tried starting the runbook without defining any parameters (basically deleting the parameter line from the script), which failed with the same error. After looking at the failed request logs, apparently if parameters are defined in the runbook, then you have to pass them.

      So, I'll create a new runbook that doesn't require any input and attempt running again without passing parameters.

      Thanks,
      Roland

      Delete