Remote Automation web service – Sitecore PowerShell Extensions pt. 3

Reason

Besides the PowerShell Console and the Integrated Scripting Environment included in the Sitecore PowerShell Extensions, the module provides a range of web services.
One of these is the RemoteAutomation web service, which offers us a single method called ExecuteScript to interact with. As the name suggests, it can be used to execute any piece of PowerShell script or other type of console command on the remote server.

The following is nothing more than a short guide to adding a reference to the RemoteAutomation web service and some sample interactions with it. Using it to run commandlets is discussed in Integration tests using PowerShell and PowerShell commandlets.

Examples are based on .NET 4.5, Sitecore 7.1 rev. 130926 and Sitecore PowerShell Extensions 2.5.1.

Note: This is part 3 of 3 regarding the concept of using the RemoteAutomation web service in the creation and deletion of test content as part of a integration test suite.

Code

The RemoteAutomation web service is located at “[domain]/Console/Services/RemoateAutomation.asmx”. Adding it as a service reference requires the usual steps as shown below.

Add a service reference - step 1

Add a service reference - step 2.1

Make sure to put some thought into the namespace used, type visibility, collection types etc.

Add a service reference - step 2.2

Example

Shown below is an interaction with the web service using it’s test page, invoking the console command “driver query” on the server.

RemoteAutomation web service invocation

The “| select” appended to the console command writes the command’s output to PowerShell. This in turn becomes part of the web service call result:

RemoteAutomation web service result

The result is always wrapped in XML as seen above, made accessible as a collection of name-value pairs when interacting with the service programmatically:

using System.Collections.Generic;
using System.ServiceModel;
using System.ServiceModel.Channels;
using ReasonCodeExample.PowerShell.Services;

public class Program
{
  public static void Main(string[] args)
  {
    string script = System.Console.ReadLine();
    Binding binding = new BasicHttpBinding();
    EndpointAddress address = new EndpointAddress("http://localhost/Console/Services/RemoteAutomation.asmx");
    RemoteAutomationSoapClient remoteAutomationService = new RemoteAutomationSoapClient(binding, address);
    // Do not pass null as the last parameter.
    List<NameValue> response = remoteAutomationService.ExecuteScript("admin", "b", script, string.Empty);
    foreach (NameValue nameValue in response)
    {
      System.Console.WriteLine("{0}\t{1}", nameValue.Name, nameValue.Value);
    }
    System.Console.ReadLine();
  }
}

RemoteAutomation web service programmatic invocation

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s