Saturday, June 2, 2012

Get all Running Runbook Jobs

This post I'll show how to get all Running Runbook jobs.

I used the ConvertTo-SCOObject function Robert Hearn posted here to parse the xml response from the GET call.

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

function ConvertTo-SCOObject {
[CmdletBinding()]
PARAM (
[Parameter(Mandatory=$true)]
[String] $XmlFeed
)
$xml =[xml]$XmlFeed

$ns = New-Object Xml.XmlNamespaceManager $xml.NameTable
$ns.AddNamespace( "d", "http://schemas.microsoft.com/ado/2007/08/dataservices")
$ns.AddNamespace( "m", "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata")
$ns.AddNamespace( "def", "http://www.w3.org/2005/Atom")

$objarray = @()

$entryNodes = $xml.SelectNodes("//def:entry",$ns)
foreach ($entryNode in $entryNodes)
{
$obj = New-Object PSObject
$objectType = $entryNode.SelectSingleNode(".//def:category", $ns).GetAttribute("term")

$obj | add-member Noteproperty -Name "Object Type" -Value $objectType.Split(".")[-1]

$propertiesNode = $entryNode.SelectSingleNode(".//m:properties", $ns)
foreach($propertyNode in $propertiesNode.ChildNodes)
{
$obj | add-member Noteproperty -Name $propertyNode.LocalName -Value $propertyNode.get_InnerText()
}

$links = $entryNode.SelectNodes(".//def:link", $ns)
foreach ($link in $links)
{
$obj | add-member NoteProperty -Name $link.title -Value $link.href
}
$objarray += $obj
}

return $objarray

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

Due to the paging limits on the web service collections (Jobs being 500), I used this method for filtering the results.  The most important part of this GET is building the query into the odata URI.  You can build in filters, select statements, etc. on the Web Service collections shown like this.

http://scorch.domain:81/Orchestrator2012/Orchestrator.svc/Jobs()?$filter=Status eq 'Running'
\_______________________________________________________/ \____/  \_________________________/
                           |                                |                   |
                    service root URI                  resource path        query options

Putting this into practice w/ SCOrch, you can query the Jobs collection, filter for Jobs that have a Status of "Running", and select the fields you would like to return – Id,RunbookId,LastModifiedTime,Status.

"http://scorch.domain:81/Orchestrator2012/Orchestrator.svc/Jobs()?`$filter=Status eq 'Running'&`$select=Id,RunbookId,LastModifiedTime,Status"

So you can use this on any of the collections to filter results i.e. Jobs, Runbooks, RunbookInstances, Statistics, etc.

The GET request would look like this.

$creds = Get-Credential("domain\username")

$XmlFeed = $null
$count = 0
$url = "http://scorch.domain:81/Orchestrator2012/Orchestrator.svc/Jobs()?`$filter=Status eq 'Running'&`$select=Id,RunbookId,LastModifiedTime,Status"
$request = [System.Net.HttpWebRequest]::Create($url)
$request.Credentials = $creds
$request.Timeout = 60000
$request.ContentType = "application/atom+xml,application/xml"
$request.Method = "GET"

$response = $request.GetResponse()
$requestStream = $response.GetResponseStream()
$readStream=new-object System.IO.StreamReader $requestStream
$XmlFeed = $readStream.ReadToEnd()
$readStream.Close()
$response.Close()

Using the ConvertTo-SCOObject function, you can then parse the response and set your variables and get a count of the running runbooks.

ConvertTo-SCOObject -XmlFeed $XmlFeed | %{$count++}
$Id = ConvertTo-SCOObject -XmlFeed $XmlFeed | %{$_.Id}
$RunbookId = ConvertTo-SCOObject -XmlFeed $XmlFeed | %{$_.RunbookId}
$LastModifiedTime = ConvertTo-SCOObject -XmlFeed $XmlFeed | %{$_.LastModifiedTime}
$Status = ConvertTo-SCOObject -XmlFeed $XmlFeed | %{$_.Status}

Here is the complete script for getting all Running Runbook Jobs.  Highlighted fields would need to be updated per your environment.

##########################################################################################################
function ConvertTo-SCOObject {
[CmdletBinding()]
PARAM (
[Parameter(Mandatory=$true)]
[String] $XmlFeed
)
$xml =[xml]$XmlFeed

$ns = New-Object Xml.XmlNamespaceManager $xml.NameTable
$ns.AddNamespace( "d", "http://schemas.microsoft.com/ado/2007/08/dataservices")
$ns.AddNamespace( "m", "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata")
$ns.AddNamespace( "def", "http://www.w3.org/2005/Atom")

$objarray = @()

$entryNodes = $xml.SelectNodes("//def:entry",$ns)
foreach ($entryNode in $entryNodes)
{
$obj = New-Object PSObject
$objectType = $entryNode.SelectSingleNode(".//def:category", $ns).GetAttribute("term")

$obj | add-member Noteproperty -Name "Object Type" -Value $objectType.Split(".")[-1]

$propertiesNode = $entryNode.SelectSingleNode(".//m:properties", $ns)
foreach($propertyNode in $propertiesNode.ChildNodes)
{
$obj | add-member Noteproperty -Name $propertyNode.LocalName -Value $propertyNode.get_InnerText()
}

$links = $entryNode.SelectNodes(".//def:link", $ns)
foreach ($link in $links)
{
$obj | add-member NoteProperty -Name $link.title -Value $link.href
}
$objarray += $obj
}

return $objarray
}

$creds = Get-Credential("domain\username")

$XmlFeed = $null
$count = 0
$url = "http://scorch.domain:81/Orchestrator2012/Orchestrator.svc/Jobs()?`$filter=Status eq 'Running'&`$select=Id,RunbookId,LastModifiedTime,Status"
$request = [System.Net.HttpWebRequest]::Create($url)
$request.Credentials = $creds
$request.Timeout = 60000
$request.ContentType = "application/atom+xml,application/xml"
$request.Method = "GET"

$response = $request.GetResponse()
$requestStream = $response.GetResponseStream()
$readStream=new-object System.IO.StreamReader $requestStream
$XmlFeed = $readStream.ReadToEnd()
$readStream.Close()
$response.Close()

ConvertTo-SCOObject -XmlFeed $XmlFeed | %{$count++}
$Id = ConvertTo-SCOObject -XmlFeed $XmlFeed | %{$_.Id}
$RunbookId = ConvertTo-SCOObject -XmlFeed $XmlFeed | %{$_.RunbookId}
$LastModifiedTime = ConvertTo-SCOObject -XmlFeed $XmlFeed | %{$_.LastModifiedTime}
$Status = ConvertTo-SCOObject -XmlFeed $XmlFeed | %{$_.Status}
##########################################################################################################

A few good resources for working with the Orchestrator Web Service.

http://blogs.technet.com/b/scorch/archive/2011/06/17/fun-with-the-orchestrator-2012-beta-the-web-service-and-powershell.aspx

http://www.odata.org/developers/protocols/uri-conventions

5 comments:

  1. Nyc info..
    Government Jobs offer an opportunity to turn your aspirations into reality. If you aspire to work in the mainstream and be a part of the nation's economic development, a job in the Government Sector may be the best option for you. Every year a number of job opportunities become available in the Government Sector.
    for more info:- sarkarijob.ind.in

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Nice and informative....In general, most of the RRB Recruitment 2020 exists in Railway Recruitment Board are for candidates possessing ITI, Diploma, BE/ B.Tech, B.Sc, B.Com, MCA, MBA, MBBS and other relevant qualifications...

    ReplyDelete