Archive for the 'SDK' Category

Creating your first PowerGUI Script Editor Add-On

We’ve published a step by step tutorial on creating add-ons for PowerGUI Script Editor. It takes you from the obligatory ‘Hello World’ example (I am sure you have always wanted PowerGUI Editor to show this at start-up! :)) all the way through creating a useful add-on (actually the Clear Console add-on we have) with menu item, proper clean up code, and so on.

We also made available full add-on SDK which documents all the APIs we expose.

These are more powerful than ISE extensibility points. Giving you the ability to redefine (or intercept and ammend) any standard functionality in PowerGUI Script Editor, add your own menu and toolbar items (anywhere), event-handlers, and even custom dockable panes within the editor. With the SDK we provide, the sky is really the limit.

PowerGUI Script Editor is one of the richest PowerShell IDEs out there and add-ons take it to the next level. We already have some really useful add-ons – and hopefully the tutorial and SDK can help you add whatever functionality you were missing.

Give them a try and let us know what you think!

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!


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

September 2022
M T W T F S S
 1234
567891011
12131415161718
19202122232425
2627282930  

%d bloggers like this: