My First MobileShell Client

In-browser PowerShell command-line is not the only way to take advantage of the PowerShell-running web server.

With the help of the PowerGUI MobileShell SDK, you can build your own MobileShell clients and use all the same web services MobileShell is using for a totally different user experience. For example, you could build your own native iPhone, Android, Blackberry, or Windows Mobile clients, Silverlight PowerShell applications and so on.

Here’s a quick step-by-step tutorial to get started with the SDK. For simplicity sake I will be using Visual Studio 2010 (Release Candidate) and build a console application. It should be quite easy for you to do the same in your favorite development environment of choice.🙂

So let’s get started. Open Visual Studio, and create a New Project / Console Application (Visual C#) – I will call mine MyMobileShellConsole:

Then, on the Project menu, click Add Service Reference. For simplicity sake, click the Advanced button on that dialog box, and then click the Add Web Reference button at the bottom of the Advanced options screen.

In the Url edit box, provide the URL of the web services endpoint for your MobileShell installation: e.g. http://my-server-name/MobileShell/MobileShell/MobileShellWebService.asmx or https://my-server-name/MobileShell/MobileShell/MobileShellWebService.asmx (mind http or https). You will then be prompted for credentials of the account which has access to the server.

Also, change the Web reference name to something more meaningful – e.g. MobileShell.

Now we can open the Program.cs file (obviously, if you are not writing a console application) you will have something else.

Add a ‘using’ clause to include the MobileShell namespace:

using MyMobileShellConsole.MobileShell;

And run our ‘Hello World’ script in the main function:

// Connect to MobileShell web service endpoint
MobileShellWebService webService = new MobileShellWebService();
webService.CookieContainer = new System.Net.CookieContainer();
webService.Url = 
  "http://MyServer/MobileShell/MobileShell/MobileShellWebService.asmx";
webService.Credentials =
  new System.Net.NetworkCredential("MyAccount", "MyPassword");

 

// Execute our first script
ScriptState state = webService.ExecuteScript("'Hello World'");
System.Threading.Thread.Sleep(1000);

// Retrieve results
state = webService.GetCurrentScriptState();
Console.WriteLine((state.Command as WriteCommand).Text);

 

Console.ReadLine();

Your project will probably look like this:

That is it. You can now run the application, have your ‘Hello World’ script executed at the MobileShell server and get the results back.

As a Step 2, let’s have a slightly more advanced script, which would actually prompt user for the command, keep retrieving results while they arrive, and so on – this is quite easy to do as well:


// Connect to MobileShell web service endpoint
MobileShellWebService webService = new MobileShellWebService();
webService.CookieContainer = new System.Net.CookieContainer();
webService.Url =
  "http://myserever/MobileShell/MobileShell/MobileShellWebService.asmx";
webService.Credentials =
  new System.Net.NetworkCredential("MyUsername", "MyPassword");

// Prompt user for command
Console.WriteLine("Type PowerShell command to execute: ");
String strScript = Console.ReadLine();

// Execute it
ScriptState state = webService.ExecuteScript(strScript);

// Keep retrieving results until execution is completed
while (state.State != PipelineState.Completed)
{
    System.Threading.Thread.Sleep(100);
    state = webService.GetCurrentScriptState();
    if (state.Command != null) {
    Console.WriteLine((state.Command as WriteCommand).Text);
    }
}

webService.TerminateCurrentScript();
state = webService.GetCurrentScriptState();
Console.WriteLine((state.Command as WriteCommand).Text);

Console.ReadLine();

With this one, you can get a bit more fancy:

And there’s more! You can use web services to retrieve intellisense information, manipulate favorites, format output, and so on. See more examples here and build your own MobileShell clients and any PowerShell applications which you need running on PowerShell-less devices!

2 Responses to “My First MobileShell Client”


  1. 1 Tom January 2, 2012 at 8:48 am

    Hi,

    are there and BlackBerry Clients out there yet?
    (sorry I don’t have any programming experience)
    Thanks!

    • 2 Dmitry Sotnikov January 3, 2012 at 11:31 pm

      Tom,

      As of version 3.1, PowerGUI Pro MobileShell natively supports Blackberry OS 5.0 and 6.0 – so no need for any custom development. Should work just fine out of the box!

      Dmitry


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




My Recent Tweets

Legal

The posts on this blog are provided “as is” with no warranties and confer no rights. The opinions expressed on this site are mine and mine alone, and do not necessarily represent those of my employer - WSO2 or anyone else for that matter. All trademarks acknowledged.

© 2007-2014 Dmitry Sotnikov

March 2010
M T W T F S S
« Feb   Apr »
1234567
891011121314
15161718192021
22232425262728
293031  

%d bloggers like this: