Admin Dangerously: Spin Up a Disposable Webserver with PowerShell

Posted
Comments None

For day in and day out administration, we should engage in best practices and encourage our colleagues to do the same. Write scripts for longevity and legibility; comment thorougly; test, test, and test again. Sometimes, though, we just need a snippet of PowerShell to live fast and die young.*

Recently at work I was faced with a challenge: our technicians were going to be going to a few dozen computers, logging in locally, and performing some specific steps. We developed a process and accompanying documentation for them, but because they were authenticating locally, there would be no easy access to Domain-gated resources, including a soft copy of the documentation itself (for copy-paste). The solution seemed to be a to put the instructions on a temporary webserver with anonymous auth, and then blow it away at the conclusion of the maintenance.

IIS would work fine for this, but I had so far not had a reason to install IIS on the 2012 server that hosts my customized reports and other scheduled tasks and, besides, that would not be much fun. PowerShell and easy access to .NET classes to the rescue!

The core of the disposable webserver is the .NET [HttpListener] class. The only real “innovation” my script offers over similar online examples is that it utilizes the “GetContextAsync()” method, versus the typical “GetContext().” This lets us easily start and stop the service interactively, so we can inspect any of the objects at will. This can be a fun way to learn both about PowerShell and web services.

To learn the most from it, I suggest creating a simple index.html file in the same location as the script, and firing it up in the PowerShell ISE (as admin—it will not work othewise). At any point, hit CTRL-C or the ISE stop button to stop the server and interact with the objects the script has created. Note that you will need to stop the listener by executing the following in the shell before you can restart the script.

$listener.Stop()

For some fun, try changing the authentication to NTLM or Negotiate and see how that changes the $context variable. This can be a very practical way to learn about claims and tokens. Add a query in the URL when you make the request. Try returning JSON formatted data and consuming it from PowerShell with Invoke-RESTMethod. Try Posting data.

Just make sure you close the console window when you’re done because, after all, we’re Adminning Dangerously.

*The author does not admin dangerously. The author wishes to express that this is a figure of speech and there are many more dangerous things people regularly do than this simple, temporary script that provides read-only information. The author does not condone any actual dangerous adminning that readers of this article may undertake.

Author

Comments

There are currently no comments on this article.

Comment

Enter your comment below. Fields marked * are required. You must preview your comment before submitting it.





← Older Newer →