8 oct. 2011

Start a System Center Orchestrator 2012 Runbook through its webservice

Article updated the 26/11/2011 (to fix a bug with the Csharp and Orchestrator parameters)


With System Center Orchestrator 2012, you have the ability to connect to it through a web service.
Jakob Gottlied Svendsen is showing how to access to it:
  • to create an excel report
  • or to create a webpart for sharepoint 2010

I would like to show you how to write a simple C# application to access to this web service and execute a runbook with parameters.


1. Generating Custom Class from the web service
To be able to use the web service in .net, you will have to create a class file, containing the format and content of what the web service has to offer.
This is done by using a console application in .NET framework 3.5 called “DataSvcUtil”
  • Open a commandprompt
  • Run this command. The command creates a .CS file in the format of the web service.
%windir%\Microsoft.NET\Framework\v3.5\DataSvcUtil.exe” /dataservicecollection /version:2.0 /language:CSharp /out:ScorchEntities.cs /uri:http://orchestrator.server.com:81/Orchestrator.svc
  • Copy or move the file to the project folder
  • Add the file to the project (add>Existing Items)


2. Let's use the class to access to the web service
Ok, now you are ready to access to your class and so the webservice.
1. Connect webservice context using
Microsoft.SystemCenter.Orchestrator.WebService.OrchestratorContext
2. Retreive the runbook It is a generic method to access to a web service. You can have some help from the microsoft website http://msdn.microsoft.com/en-us/library/cc668796.aspx
var runbooks = from runbook in ScorchSVC.Runbooks

                           where runbook.Name == runbookname
                           select runbook;
3. You need to retreive the also all runbook's parameter.
            var runbookparamaters = from runbookparamater in ScorchSVC.RunbookParameters
                                    where runbookparamater.RunbookId == runbooks.Single().Id
                                    select runbookparamater;
4. Create a job
var job = Microsoft.SystemCenter.Orchestrator.WebService.Job.CreateJob(System.Guid.NewGuid(), runbooks.Single().Id, "user1", DateTime.Now, "user1", DateTime.Now);
5. Pass the parameter to job. I think it is the hardest thing. You need to know the format. In the step3, we have retreived the runbookparameterid and we will use it. The parameter Name is not enough, you need to have the id...
<Data>
<Parameter>
<Name>Parameter Name for example arg1 </Name>
<Id>Guid of the parameter so guid of arg1</Id>
<Value>the value :)</Value>
</Parameter>
<Parameter>
etc.
</Parameter
</Data>
6. Last step is to passe the job created to the context and apply the change.

            ScorchSVC.AddToJobs(job);

            ScorchSVC.SaveChanges();





Here is the full source code. Let me know if you want more information...





using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace SCO_Runbook
{
    class Program
    {


        public void ExecuteRunbook(string webserviceuri, string runbookname, List<string> parameters)
        {
            var ScorchSVC = new Microsoft.SystemCenter.Orchestrator.WebService.OrchestratorContext(new Uri(webserviceuri));
            ScorchSVC.Credentials ) new System.Net.NetworkCredential();
            // You can specify som custom credentials.
            // ScorchSVC.Credentials = new System.Net.NetworkCredential("user", "password", "domain");
            var runbooks = from runbook in ScorchSVC.Runbooks
                           where runbook.Name == runbookname
                           select runbook;
            var runbookparamaters = from runbookparamater in ScorchSVC.RunbookParameters
                                    where runbookparamater.RunbookId == runbooks.Single().Id
                                    select runbookparamater;


            var job = Microsoft.SystemCenter.Orchestrator.WebService.Job.CreateJob(System.Guid.NewGuid(), runbooks.Single().Id, "user1", DateTime.Now, "user1", DateTime.Now);
            int count = 0;
            string strParam = "<Data>";
            foreach (var item in runbookparamaters)
            {
                strParam += "<Parameter><Name>" + item.Name + "</Name>";
                strParam += "<ID>{" + item.Id + "}</ID>";
                strParam += "<Value>" + parameters[count] + "</Value></Parameter>";
                count++;


            }
            strParam += "</Data>";
            job.Parameters = strParam;
            ScorchSVC.AddToJobs(job);
            ScorchSVC.SaveChanges();


        }
        static void Main(string[] args)
        {


            List<string> myParameters = new List<String> { };
            // in the example below, you would like to start the runbook called rbk1
            // The runbook rbk1 has 1 parameter
            // called arg1 and we would like to affect 5
            // So we run rbk1 with arg1=5
            myParameters.Add("5");
            ExecuteRunbook("http://orchestrator.server.com:81/Orchestrator.svc/", "rbk1", myParameters);
        }
    }
}

3 commentaires:

brunosa a dit…

If needed, you can also use this tool to start the runbook remotely from a UI or from a command line : http://blogs.technet.com/b/yasc/archive/2011/11/17/orchestrator-remote-tools-2-0-fka-the-opalis-ui-generator.aspx
hope this helps

laurent DB a dit…

Really good dev! Brunosa..

Anonyme a dit…

An impressive share! I have just forwarded this onto
a colleague who had been conducting a little homework on this.
And he in fact bought me dinner simply because I stumbled
upon it for him... lol. So allow me to reword
this.... Thank YOU for the meal!! But yeah, thanks for spending the time to talk about this topic here on your web site.


Also visit my web-site :: Louis Vuitton