HOME   |   CONFIGURATION   |   DOWNLOAD   |   LICENSE   |   FAQ
How to configure PowerShellASP

You can use PowerShellASP in any regular ASP.NET application project by following these simple steps:

1. Add a reference to the PowerShellASP assembly: PowerShellToys.PowerShellASP.dll

2. Add an entry in your Web.config file mapping the *.ps1x extension to the PowerShellASP assembly, like this:


3. If you have not done so before, configure the *.ps1x extension to be mapped to the ASP.NET ISAPI extension library in your IIS application

 After following these simple steps you’ll be ready to create PowerShellASP pages in your Web Application.


Authoring PowerShellASP pages

PowerShellASP pages are simple text files with the *.ps1x extension that contain both markup as well as snippets of regular PowerShell code interacting together. Unlike ASP.NET, there’s no “code behind” model for PS1X pages; in this sense they resemble more the ASP classic model.

Here’s a very simple PS1X page:

As you can see, everything is HTML markup right until the <%= %> section, which means “evaluate this PowerShell expression and print the result”. The expression, in this case, is using the intrinsic ASP.NET Request object to query data coming in the query string of the URL.

You can also create full code blocks that include any other kind of PowerShell expression or flow control construct, and even intermingle that with markup code. For example, here’s a simple page that will present the list of running processes on the machine:

ASP.NET Intrinsic Objects

Besides running standard PowerShell code, you will want to interact with the HTTP runtime through the use of the ASP.NET Intrinsic objects like HttpRequest and HttpResponse. Because of the threading model used by PowerShell, those objects aren’t accessible directly through HttpContext.Runtime.

  • $Request: Contains the HttpRequest object.

  • $Server: Contains the HttpUtility object.

  • $Session: Contains the HttpSession object.

  • $Application: Contains the HttpApplication object.

  • $Response: Contains the HttpResponse object. You can write directly to the response stream from code if you want, or you can use write-host and friends as well.

  • $Cache: Contains the HttpCache object.

  • $Context: The HttpContext object associated with the current request.

        Using these objects works exactly the same as in regular ASP.NET applications. Here’s an example script that will dump all values in the HttpRequest object to a simple HTML page:

You can see this sample uses less of the templating capabilities in PowerShellASP and uses simple PowerShell expressions to generate the output, using the write cmdlet, and this will work just as well.




© Copyright 2008 PowerShellToys - All Rights Reserved.