Fun with the Orchestrator 2012 Beta,the Web Service and PowerShell

The Orchestrator 2012 Beta is barely “out of the oven” and I have some fun PowerShell stuff for you to play with using the new REST-based web service. Working with web services from PowerShell is rather new to me, but I was surprised how easy it was to create a few functions that let me browse around the contents of the data in Orchestrator using the web service. Get the PowerShell script for all of this here .

First of all, I created a function to do the actual connection to the web service and querying it. Thanks to another smart person for coming up with this part (see my previous post where I talked about this). This function is called Query-RestService, This is a generic function that allows me to call virtually any REST-based web service. On top of that, I have another function called Query-SCOService. The Query-SCOService function simply formats the URI properly for the SCO web service (which you could do yourself with the Query-RestService function). If I call this function and don’t provide a query, I get the same thing I’d get if I typed the default web service URL into a browser:

Web Service Response

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<service xml:base="https://sco12-rh1:81/Orchestrator.svc/" xmlns:atom="https://www.w3.org/2005/Atom" xmlns:app="https://www.w3.org/2007/app" xmlns="https://www.w3.org/2007/app">
  <workspace>
    <atom:title>Default</atom:title>
    <collection href="Folders">
      <atom:title>Folders</atom:title>
    </collection>
    <collection href="Runbooks">
      <atom:title>Runbooks</atom:title>
    </collection>
    <collection href="RunbookParameters">
      <atom:title>RunbookParameters</atom:title>
    </collection>
    <collection href="Activities">
      <atom:title>Activities</atom:title>
    </collection>
    <collection href="Jobs">
      <atom:title>Jobs</atom:title>
    </collection>
    <collection href="RunbookInstances">
      <atom:title>RunbookInstances</atom:title>
    </collection>
    <collection href="RunbookInstanceParameters">
      <atom:title>RunbookInstanceParameters</atom:title>
    </collection>
    <collection href="ActivityInstances">
      <atom:title>ActivityInstances</atom:title>
    </collection>
    <collection href="ActivityInstanceData">
      <atom:title>ActivityInstanceData</atom:title>
    </collection>
    <collection href="RunbookServers">
      <atom:title>RunbookServers</atom:title>
    </collection>
    <collection href="RunbookDiagrams">
      <atom:title>RunbookDiagrams</atom:title>
    </collection>
    <collection href="Statistics">
      <atom:title>Statistics</atom:title>
    </collection>
  </workspace>
</service>

The really good stuff comes with the ConvertTo-SCOObject function. This function takes the XML output from the web service query and converts it into custom PSObjects that look like this:

Sample "Job" Object

Object Type      : Job
Id               : 2976c7c0-860c-48d0-b102-257a59ae4493
RunbookId        : e042d1f6-040a-48a7-a2b5-b850d1145c9c
Parameters       : <Data/>
RunbookServers   :
RunbookServerId  : 3af5371d-3ba2-49f5-83ad-0264d2427481
ParentId         : e950b167-cf82-434f-a726-160d05ab15b8
ParentIsWaiting  : false
Status           : Completed
CreatedBy        : S-1-5-500
CreationTime     : 2011-06-11T15:21:44.653
LastModifiedBy   : S-1-5-500
LastModifiedTime : 2011-06-11T15:21:46.49
Job              : Jobs(guid'2976c7c0-860c-48d0-b102-257a59ae4493')
Runbook          : Jobs(guid'2976c7c0-860c-48d0-b102-257a59ae4493')/Runbook
Instances        : Jobs(guid'2976c7c0-860c-48d0-b102-257a59ae4493')/Instances
RunbookServer    : Jobs(guid'2976c7c0-860c-48d0-b102-257a59ae4493')/RunbookServer
Statistics       : Jobs(guid'2976c7c0-860c-48d0-b102-257a59ae4493')/Statistics

I can create one object or an array of these objects easily, just by doing the following:

(1)   Create an object containing the XML output from the web service query in Atom put feed format:

$xmlfeed = Query-Rest -scoserver "sco12-rh1" -credentials $cred -query "Jobs?$filter=RunbookId%20eq%20guid'9296a3c2-29b1-4000-a155-e8e3adc855e9'"

(2)   Pass this xml into the ConvertTo-SCOObject function

ConvertTo-SCOObject -XmlFeed $xmlfeed

The resulting output is one or more objects that can then be used for further querying. For example, if I want to get a list of all Instances of a job that do not have a status of “completed” and show only their IDs, I could do this:

Query-SCOService -scoserver "sco12-rh1” -credentials $cred -query (ConvertTo-SCOObject -XmlFeed $xmlfeed | ? {$_.Status -ne "Completed"}).Instances | Select ID

Do you see how I am utilizing the properties as extensions to the base URI? Cool, huh?

I whipped this whole thing up in a couple of hours. I encourage you to play around with it and try new things. When we ship Orchestrator 2012, we’ll have an official, shipping PowerShell interface that’s built on top of the web service, and it will definitely be much better than this simple script, but between now and then this is something to get you thinking about how useful PowerShell and the new web service are together.

Get the PowerShell script for all of this here

Enjoy!