I will have to start this article with a real world example, by posing a question:
“What if you build something so magnificent that you no longer spend all your time ensuring the uptime of your environment?”
To explain, I just recently completed a fairly large upgrade from 2007 Exchange and OCS 2007R2 to a fully site resilient PA aligned solution that thanks to careful and proper planning by myself and the other individuals involved resulted something that no longer requires much TLC.
So, my environment is now extremely manageable, with very little complexity and fully HA, allowing for all the fun things in life such as fully automated patching cycles and no more late night change controls.
In my scenario this left me with 4 basic requirements:
1. I want to be able to a look at a real-time view of the environment at any point in time and be able to spot potential problems immediately.
2. I don’t want to have to memorize every single PowerShell command in the world to always be able to perform any required task without having to reverence my notes, get-help or even a web search for the answer.
3. I want to empower the people who are tasked with performing actions in my environments consistently and correctly.
4. I don’t want to be dependent on a product rollout or configuration or implementation to meet my needs.
Without going into the details of all the various methods I have tried to achieve what I wanted whilst being able to satisfy all 4 needs, I will jump straight into the solution.
THE SOLUTION: PowerShell within ASP.NET
My final choice is the result of months of thinking, wondering, wanting and also testing various methods available to me whilst remaining within the boundaries of my 4 core requirements.
To go into a slight level of depth, I played with various options, from writing out HTML code via powershell and running it on schedules and putting all my output in a central location hosted within a iframe on a website, to attempting to revive poshboard and various other fun things such as exporting data to csv and importing to excel and visio but yet somehow nothing ever produced the results I wanted.
So let us call this PART 1: How to turn an ordinary website into a powershell interface?
STEP 1: Find a copy of Visual Studio – We want to create an ASP.NET Web Form Page with some packages installed using NUGET.
STEP 2: Once installed we literally need to create ASP.NET web application using forms, it can be a standalone website or a web application that portion does not matter.
STEP 3: We need to host this application somewhere, and for purposes of simplicity for initial testing we can use a service or testing account to host the application pool running the website to reduce initial configuration requirements.
WHAT HAPPENS NEXT?
We create an empty page using the designer view on visual studio and we place 3 items on the page.
1 x Textbox labelled “input”
1 x Textbox labelled “output”
1 x Button labelled “Execute”
This basically looks like this:
Next we need to install our required packages, I used NUGET since it was easy, but ultimately all we really need to proceed is the “system.management.automation” package.
Once this is installed, we merely double click on our execute button above which takes us to the “CODE BEHIND” portion of our page, which is basically C#, which looks a little like powershell.
For anyone not knowing what goes on here, much like myself, this is what I have gathered so far:
Top of the .cs file defines the modules and the page
Next we have a section of actions to be performed when the page is loaded
In the remaining section we have actions such as button click execution code as well as anything else relevant to the page to be defined and or executed.
For our basic example we will import the system.management.automation module at the top and produce some code for allow our button to execute our input field command and write it into the output box:
Our end result is simple, a web site from where to run powershell commands using the elevation we chose to assign to the application pool, or alternatively, running on our local machine using the preview on visual studio, a sample of our basic text results:
This concludes the basic introduction to making this idea turn into a reality.
There will be some time spent on creating pages and interfaces and many hours stuck trying to figure out why something does not work, however the frustration for me personally thus far has been worth every second.
At this point my project has resulted in a 13 function page with dropdown selections where applicable and utilizing the ‘.visible = true;” or “false” statements to show and hide buttons and boxes and text labels on a single page, driven by a dropdown to allow operators to perform tasks based on an email input.
I look forward to writing the next part of this article, in which I will show the code to manipulate the powershell output data.
Feel free to contact me for a sample project file or portions of the code, I found manipulating a working piece of code in these early stages to be a lot less effort than attempting to construct something from scratch.