Starting at 12th of September 2011 I’ll be freelancer. – In August I had some vacation days to spend. – So I created a new project for me – and for you.

I call it “Web based PowerShell Console” or “WebPSConsole”.

It is a full featured Browser based PowerShell console that enables you to work on a server machine remotely. It’s like PowerShell remoting but it’s not the same. While PowerShell remoting uses “WinRM” the tool that I’ve created uses a “normal” PowerShell host that will be executed in an IIS environment. (Please see my comment related to “PowerGUI Pro Mobile Shell” at the end of this article.)

That means: With my tool you will have a ASP.NET 4.0 based Web Application that can be accessed by a browser. The ASP.NET web app has a .ASPX site and some code behind. On the server in the ASP.NET context a PowerShell Host developed by me is running that accepts commands send by the clients Browser. If a command is send it will be executed by the PowerShell Host. All output is send back to the clients Browser.

It’s on Codeplex: http://webpsconsole.codeplex.com with all source code! – It’s ALPHA. I’ve done lots of testing but I’m a single developer with a single machine… I’d be happy to get your improvements or experiences!

Lets have a look at the app:

That is the “GUI”. The black frame will contain the output of the server side PowerShell session.

Let’s enter a command.

get-childitem c:\windows | select -first 5

After clicking “Send” the command is send to the server. The server executes the command and returns the PowerShell output.

Let’s try this:

get-credential

You see the blue colored input box for the credential information input.

Here you see the implemented “session timeout”:

After 15 minutes of inactivity you’ll get a warning. After 20 minutes the session will be terminated. After termination you are not able to access the old session including session history!

Let’s try this:

a$ = read-host

Here you get an input field for a single line of text. The output is stored in variable $a. Let’s check the content of variable $a after sending the input:

Now let’s test the functionality of completing missing cmdlet parameters:

get-content

Leave the last line empty and click “Send”.

You get:

Now let’s test the “choice” functionality:

After “Send” you get:

You see here the blue colored choice input field. If you choose “Halt Command” in this case the “Hello Ingo” command will not be executed:

If you select “Cancel” in such cases the complete PowerShell pipeline will be stopped! Not only the current command!

You can set colors a you need to:

Than “Send” and test it with:

You’ll get:

Now let’s have a look at the function “Download console as RTF”. You’ll find the link above the console frame.

Click “Open”.

This is a Rich Text Format copy of the complete console output! – A “Clear-Host” will not clear this output! – This file can only be downloaded while the current session running. After the session ends you will not be able to access the information anymore!

Let’s try “Show current buffer”. This command is above the console frame too.

Here you get the “real” PowerShell buffer as HTML. This page can be save.

Let’s try:

clear-host

Open “Show current buffer” again:

(It’s empty now because clear-host did clear the PowerShell buffer!)

A word to the keyboard usability:

In the command input field you can use ESCAPE to clear the input field.

You can use CTRL+ENTER to send the command.

In other input fields (choice, credential, read-line,…) except “Read-Key” (see below) you can use ESCAPE to cancel the current operation and pipeline.

You can use there ENTER to send your entered data.

You can use TAB and SHIFT+TAB to navigate inside the input frame: input field, Send button and Cancel button.

The “Trace log” is an optional frame that maybe shows more information about your server connection and the “work behind”. Just click the line and the frame will be shown:

(“Send buffer as HTML” is caused by the “Show current buffer” function.)

You can close the PowerShell session by clicking the “x” in the dialog title (beside “v0.1.0.0”):

Now you can close the Browser or click “Ok” to start a new session. Or reload the Browser page to create a new session.

Setup

Now I want to tell you how to install the project.

First of all you need IIS. This can be installed on Windows 7 too.

You need a user account for executing PowerShell at server side. This account is used by every user of WebPSConsole: Each user of WebPSConsole will be impersonated at the server with the “execution account”. Here you should choose the account very carefully.

1.

Create a directory on the server where you store the binaries. You create a folder “c:\inetput\webpsconsole”.

2.

Copy the binaries there.

3.

Open IIS Manager.

Create a new Application Pool for the “execution account”. This account need to execute ASP.NET 4.0 code.

Select “Classical Mode” for the application pool! – Please check the settings. In my case the settings were not used. After creating the app pool I had to edit it and set “Pipeline Mode” and “Framework Version” again!!!

4.

Create a new web application “WebPSConsole” using the previously created application pool.

Right click the “Sites” node in IIS Manager.

You get this dialog. Fill in your specific informations. Here is my sample. I’ll use “ikWebPSConsole” as host header name in this setup demo because the host header “webpsconsole” already exists for development purpose.

Click the “Select” button beside “Application pool:

“OK”.

My sample data:

(In order to get this working on my machine I have to edit c:\windows\system32\driver\etc\hosts and insert “ikWebPSConsole” as new local DNS entry.)

As I said I’ve developed this project in my last vacation. After finishing v0.1.0.0 I’ve seen PowerGUI Pro MobileShell by Quest Software. It’s based on the same idea as my project but is older and has more features I think. I only know a YouTube video of it because I do not know the commercial version of PowerGUI. – Beside this the free PowerGUI tool is my favorite PowerShell IDE. – For use on a live server you should think about using PowerGUI Pro MobileShell!

But: It does not convert the PowerShell script to an other language! It encapsulates the script with a lightweight PowerShell host written in C# and compiles the dynamically generated C# source code in memory to an EXE file. The resulting EXE is an .NET assembly that contains the source script encoded in Base64. The EXE includes all stuff that is needed to execute an PowerShell through the .NET object model. It is based on classes in the namespace System.Management.Automation that reperents the PowerShell engine. – Therefore the EXE file is not a real “standalone” EXE file. It needs PowerShell to be installed!!! And – of course – it needs .NET Framework v2.0. Furthermore “script execution” have to be allowed (see cmdlet: set-execultionpolicy). – The resulting EXE is “MSIL” and is able to execute as x64 or x86.

The tool “PS2EXE” itself is a PowerShell script! – It does the in-memory compilation and generates the EXE file. It uses the CSharpCodeProvider class of namespace Microsoft.CSharp.

The script is really simple. I contains a multiline string that represents the PowerShell host I’ve written. This is much more interesting than the PS2EXE.ps1 script itself. – Have a look into it!

Usage:

Call the script with this parameters:

-inputFile

PowerShell script file

-outputFile

file name (with path) for the destination EXE file

-debug

(switch) generate debug info in the destination EXE file. The dynamically generated .CS file will stored beside the output EXE file. Furthermore a .PDB file will be generated for the EXE file

-verbose

(switch) shows also verbose informations – if any.

Sample:

This creates “test.exe” out of the PowerShell source file “test.ps1”

Limitations: It’s not easy to create a fully functional PowerShell host such as “Console host” (powershell.exe) or “ISE” (powershell_ise.exe). So there may be functionality that does not work properly.

The generated EXE can also be calls using command line options. There are 4 options that are used by the PowerShell host:

-debug

Forces the EXE to be debugged. It calls “System.Diagnostics.Debugger.Break()”.

-extract:”Filename”

Extracts the PowerShell script inside the EXE and saves it as “Filename”. The script will not be executed.

-wait

At the end of the script execution it writes “Press a key…” and waits for a key to be pressed.

-end

All following options will be passed to the script inside the EXE. All preceding options are used by the EXE and will not be passed to the script.

Sample:

I create a script file containing this line of code:

$args | Write-Host

I save it as “c:\test2.ps1” and convert it as EXE by using PS2EXE:

Sample 1.: “-wait” forces the “Hit any key…” message. All options following “-end” will be passed to the script.

Sample 2., 3. : The script will not get options preceding to “-end”.

Sample 4: “-wait” follows to “-end” and so it’s passed to the script. No message “Hit any key…”.

So. That’s it for the moment. Please feel free to modify the script and let me know.

For a current project I want to create a “master script” that contains another compressed PowerShell script. This compressed script contains some informations – server names, network share names – that should be as unreadable as possible for normal users. This script is executed during user logon.

I created two scripts

"compress.ps1"

"execute.ps1"

The first script is used to compress a script. The second one is used to execute the compressed script.

DISCLAIMER

The informations on my blog are provided as is and I cannot guarantee that editing the registry or executing the provided code or provided scripts or doing other actions described in my articles will not cause serious damage. You use this information at your own risk! - I'm not responsible content on external sites.