MobileShell SDK

Developers, here’s your chance to create exciting applications on top of PowerGUI MobileShell server backend! That’s right: everything you see in MobileShell including settings dialog boxes and intellisense is exposed as web services. Your application will be able to execute any PowerShell scripts in production environment over HTTP/HTTPS, preserve session state and even get meta information such as intellisense.

Which means that if you don’t like our in-browser implementation and want to develop a native iPhone/Blackberry/Symbian/Android/Windows Mobile client – you can quite easily do that (and probably even make some money out of the respective app stores ;))

Below is a sample in C# that uses the PowerGUI MobileShell API to:

  • Create, list, join and close sessions
  • Work with IntelliSense autocompletion lists
  • Manage favorite scripts

Hopefully it is straightforward enough to figure out how to perform similar calls in your development platform of choice.

For this particular example to work in Visual Studio, you need to add the MobileShell Web service as a Web reference to the .NET project you are working with. The URL of the service looks like: https://<MobileShell_server_name>/MobileShell/MobileShell/MobileShellWebService.asmx.

For details about adding Web references to Visual Studio, see http://msdn.microsoft.com/en-us/library/d9w023sx.aspx.

If you are using tools other than Visual Studio to run .NET code against the MobileShell Web service, you need to generate a Web service proxy using the wsdl.exe utility, which is distributed with .NET Framework 3.5. For details, refer to the help screen for the utility.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MobileShellExamples
{
 class Program
 {
  static void Main(string[] args)
  {
    ScriptExecutionExample();
    FavoritesExample();
    IntelliSenseExample();
    SessionsExample();
  }

  static void ScriptExecutionExample()
  {
   MobileShellWebService webService = GetWebServiceInstance();
   webService.CreateSession();

   string strScript = "while ( $true ) { $i++; \"Hello $i\"; sleep -m 300 }";
   ScriptState state = webService.ExecuteScript(strScript);

   System.Threading.Thread.Sleep(5000);

   webService.TerminateCurrentScript();

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

  static void SessionsExample()
  {
   // Create the first session
   MobileShellWebService webService1 = GetWebServiceInstance();
   webService1.CreateSession();

   // Create the second session
   MobileShellWebService webService2 = GetWebServiceInstance();
   webService2.CreateSession();

   // Get a list of active sessions
   MobileShellWebService webService3 = GetWebServiceInstance();
   SessionDetails[] arrSessionIDs = webService3.GetSessions();

   // Close all sessions except the first
   for (int i = 1; i < arrSessionIDs.Length; i++)
   {
    webService3.CloseSession(arrSessionIDs[i].SessionID);
   }
   // Join the first session
   webService3.JoinSession(arrSessionIDs[0].SessionID);
  }

  static void IntelliSenseExample()
  {
   MobileShellWebService webService = GetWebServiceInstance();

   // Get autocompletion list for the "Get-" string (e. g. get all Get-* cmdlets)
   string strScript = "Get-";
   foreach (IntelliSenseItem item in
           webService.GetIntelliSenseCompletionList(strScript, strScript.Length-1))
   {
    Console.WriteLine(item.AutoCompleteEnding);
   }
   Console.WriteLine("");

   // Get autocompletion list for "Get-Help -" string.
   // (e.g. get all Get-Help cmdlet parameters)
   strScript = "Get-Help -";
   foreach (IntelliSenseItem item in
           webService.GetIntelliSenseCompletionList(strScript,strScript.Length-1))
   {
    Console.WriteLine(item.AutoCompleteEnding);
   }
  }

 static void FavoritesExample()
 {
   MobileShellWebService webService = GetWebServiceInstance();
   // List current favorites
   Console.WriteLine("### Initial list");
   foreach (FavoriteScript cmdFavorite in webService.GetFavoriteScripts())
   {
     Console.WriteLine("Name: " + cmdFavorite.Name);
     Console.WriteLine("Script: " + cmdFavorite.Script);
     Console.WriteLine("");
   }

   // Add a new favorite script named "Hello World"
   webService.AddFavoriteScript("Hello World", "\"Hello World!\"");

   // List current favorites
   Console.WriteLine("### New favorite script added");
   foreach (FavoriteScript cmdFavorite in webService.GetFavoriteScripts())
   {
    Console.WriteLine("Name: " + cmdFavorite.Name);
    Console.WriteLine("Script: " + cmdFavorite.Script);
    Console.WriteLine("");
   }

   // Find the favorite script named "Hello World"
   FavoriteScript cmdNewScript = webService.GetFavoriteScripts().First(
                                     fav => fav.Name == "Hello World");

   // Modify the script name and content
   webService.EditFavoriteScript(cmdNewScript.Name,
                                 "Time keeper", cmdNewScript.Script);

   // List current favorites
   Console.WriteLine("### Favorite script modified");
   foreach (FavoriteScript cmdFavorite in webService.GetFavoriteScripts())
   {
    Console.WriteLine("Name: " + cmdFavorite.Name);
    Console.WriteLine("Script: " + cmdFavorite.Script);
    Console.WriteLine("");
   }
  }

  static MobileShellWebService GetWebServiceInstance()
  {
   return GetWebServiceInstance(
       "http://myserver/MobileShell/MobileShell/MobileShellWebService.asmx",
       "mydomain\\administrator", "=1qwerty");
  }

  static MobileShellWebService GetWebServiceInstance(string strUrl,
                                         string strUser, string strPassword)
  {
   MobileShellWebService webService = new MobileShellWebService();
   webService.CookieContainer = new System.Net.CookieContainer();
   webService.Url = strUrl;
   webService.Credentials = new System.Net.NetworkCredential(strUser, strPassword);
   return webService;
  }
 }
}

Detailed SDK documentation (all methods and classes) can be found in the “PowerGUI Pro MobileShell 2.0 SDK” document posted here.

Give a try and let us know what you think. And obviously whichever apps you create we will be extremely glad to promote them whichever way we can!

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

January 2010
M T W T F S S
« Dec   Feb »
 123
45678910
11121314151617
18192021222324
25262728293031

%d bloggers like this: