Executing Orchestrator runbook using ASP.net C#
I am going to go
over the code for executing Orchestrator runbook using ASP.net C#.
This is helpful
when you need to create a UI for your SCORCH runbooks.
SCORCH's native UI
the web console has limited functionality especially when you need data
validation or custom controls like calendars.
· Step 1: You need to start by adding a service reference to a Visual Studio
project
1.
In Solution
Explorer, right-click the name of the project that you want to
add the service to, and then click Add
Service Reference.
2.
In the Address box,
enter the URL for the Orchestrator web service, for example http://server1.contoso.com:81/Orchestrator2012/Orchestrator.svc.
If your current user account doesn’t have access to Orchestrator then you may
be prompted for a username and password.
3.
Click Go.
4.
In the Namespace box,
enter the namespace that you want to use for the reference, for example SCOService.
5.
Click OK.
· Step 2:
Next you need to find the GUID for your runbook. In
order to do this yiou need to query the Orchestrator Database
use
Orchestrator
select UniqueID from dbo.POLICIES
where Name like '%Runbook Name%' and Deleted = '0'
*Note the schema has changed in Orchestrator 2016, I will updating this poost
*Note the schema has changed in Orchestrator 2016, I will updating this poost
Finally you can use the C#
code below to execute . I will explain the code using C# comments //
You need a .net form with a textbox TextBox1 and
label Label1
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Net;
using System.IO;
using System.Data.Services.Client;
using SCOService; // this was added in Step 1
public partial class execRb : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
//
Details of runbook that we are going to run.
Guid runbookId = new Guid("E59934E6-5DE4-439D-BCCF-BF0EDBCDEC3E");
//
Tou should have the Guid from Step 2 in this post
Hashtable parameterValues = new Hashtable();
//
Server Name
parameterValues.Add("ServerName", TextBox1.Text);
string serviceRoot = "http://youSCORCHserver/Orchestrator2012/Orchestrator.svc";
//
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;
//
Retrieve parameters for the runbook
var runbookParams =
context.RunbookParameters.Where(runbookParam => runbookParam.RunbookId ==
runbookId && runbookParam.Direction == "In");
//
Configure the XML for the parameters
StringBuilder parametersXml = new StringBuilder();
if (runbookParams != null &&
runbookParams.Count() > 0)
{
parametersXml.Append("<Data>");
foreach (var param in runbookParams)
{
parametersXml.AppendFormat("<Parameter><ID>{0}</ID><Value>{1}</Value></Parameter>", param.Id.ToString("B"),
parameterValues[param.Name]);
}
parametersXml.Append("</Data>");
}
try
{
// Create new job and assign runbook Id and
parameters.
Job job = new Job();
job.RunbookId = runbookId;
job.Parameters =
parametersXml.ToString();
// Add newly created job.
context.AddToJobs(job);
context.SaveChanges();
Label1.Text = "Successfully
started runbook";
//success
}
catch (DataServiceQueryException ex)
{
//
Response.Redirect("Error.html");
throw new ApplicationException("Error starting
runbook.", ex);
Label1.Text = "Error starting
runbook";
}//check
null
}
}
Comments
Post a Comment