Create a Powershell Web Application

How to create a Powershell Web Application? This article will give you some guidelines on how to deploy a very simple web application that leverages Powershell and if you follow it completely, you’ll be able to have a fully functional web application.

This is a very important instrument, especially when our goal is to automate as much as possible the environment and repetitive tasks or offload a 2nd or 3rd level task to a 1st level representative. You could build a web application to check permissions for a specific shared folder (I’ve done that successfully) or you could deploy an app that would check the current top 10 RAM processes being used on a remote server (super handy to hand off to a 1st level support team who many have no access to the server) and so on.

Please see the final notes at the end of this article.

Prerequisites

You will need Visual Studio to follow this.

Creating a new Project

  • Click on File > New > Project
    • visual-studio_create-new-project
  • Select Installed > Templates > Visual C# > Web and click ASP .NET Web Application (.NET Framework). Give it a name (ITDropletsPowershell in the example).
    • visual-studio_create-new-asp.net-web-application
  • Since we want to start with a clean solution, let’s select “Empty” and click OK.
    • visual-studio_create-new-asp.net-web-application-EMPTY

Adding the needed packages to the Solution

  • Click on Tools > NuGet Package Manager > Package Manager Console
    • visual-studio-Package-Manager-Console
  • Let’s install these two packages, by running the below two lines of code:

    • This is how it’s going to look:

      visual-studio-install-packages-output

Creating the Default.aspx and the Code Behind

  • Add a New Item
    • visual-studio-add-new-item
  • Select Web Form and name it Default.aspx
    • visual-studio-new-web-form
  • Here it is:
    • visual-studio-default.aspx-brand-new

Now we have the basic to start building a web interface that will need to get our input and tweak the code behind file to let it do what we need it to do.

Web Application Example

Let’s try to build a real life example. Let’s say we want a simple form where we input a Directory (i.e. C:\Users\Username\Desktop) and the script must return us the child items.

So what we want the script to do is simply get the user to input a variable (the directory) and then call the following:

Let’s first edit the Default.aspx to look like this:

So what we changed from the original Default.aspx is:

  • Added a title “Get-ChildItem”.
  • Added an h1 Title on the page and centered “Get-ChildItem (itdroplets.com)”.
  • Added a text “Please type the directory for which you want to get the child items:” followed with a TextBox with ID Input.
  • A button with text “Run” and ID RunInput.
  • Added a text “Result” with a MultiLine TextBox with ID Result.

Time to edit the code behind. Expand Default.aspx and select Default.aspx.cs. Here’s how it’s going to look:

visual-studio-edit-code-behind

This is how my code behind looks like now, I tried adding comments so that there’s no need to repeat it below.

Let’s test this!

  • Right click on Default.aspx and select View in Browser
    • visual-studio-view-in-browser

 

Here’s our web application.

visual-studio-web-application-get-child-item

And here’s an example of the result for the folder C:\.

visual-studio-web-application-get-child-item_Test

 

Running an external Function that is stored in a Module

If you look back at the code behind above, I’ve added these lines (I removed the comments from the below):

Now those two lines will import two modules from a path under MyModuleFolder1 and MyOtherModule2. If you had a function into one of them called Sample-Function-ITDroplets, and said function would accept a parameter called “Username”, you could just called it as if you were calling Get-ChildItem!

Something like this:

You can understand that you could then go and add many other input fields in the Default.aspx page and every “variable” will be available here.

One last example

Imagine that now you want to add another parameter to the Get-ChildItem command, for instance you want to also add -Recurse. To do that, just add the following line, right under “shell.Commands.AddParameter(“Path”, Input.Text);” in the Default.aspx.cs file.

Once you run the web application again, you’ll run it in Recurse. If you’re now testing this, pay attention! If you specify a folder with a ton of files, you’ll end up waiting for quite some time!

Publishing the entire solution to an existing IIS Web Server

This is so simple and so powerful and so handy! So we’re done and we want to deploy our web application to a server we own.

  • Right click on ITDropletsPowershell and click Publish
    • visual-studio-publish
  • Select IIS, FTP, etc and then click Publish
    • visual-studio-publish-IIS-FTP-etc
  • Now add all of the info and credentials for the web-server you’re deploying this to and you’re done! Just follow the wizard through.
    • visual-studio-publish-custom-profile

Final Notes

It’s important to understand security. I will have a separated article ready soon to talk about how to secure this application we just deployed to make sure only a certain group or user can access it.

Another important factor is the ability for the account running the web application (IIS) to have access to whatever the script is doing. So the best is to run the web application with a service account and provide access to said service account. For instance, in our case with the Get-ChildItem web application, we could target a share \\itdroplets.com\myshare, but if the service account running IIS has no access to it, you’ll just get an access denied error.

I also suggest to work as much as possible on error handling, this is simple when calling external scripts as everything will be in a powershell format.

Leave a Reply

Your email address will not be published. Required fields are marked *