PoSHServer for Fun and Profit (and open-source props)

At my workplace, we use a lot of PowerShell. I mean a lot. I just did a quick script to count lines of PowerShell in my local git repos – admittedly very quick and dirty – and came up with going on for a quarter of a million. 221668 to be exact.

$x=0; gci -recurse -Filter "*.ps*1" | % { $x += (gc $_.FullName).count}; $x

And, of course, we’re a company that’s heavily bought into the API-first philosophy. Or at least the “while we’re rearchitecting, we’re doing the APIs first” philosophy. We deal with incoming and outgoing hooks and API calls a lot.

So it was kind of inevitable that eventually, the DevOps team would buy in to PoSHServer.

PoSHServer is a PowerShell-based web server that’s not only written in PowerShell, but actually runs PowerShell code as web application code*, in a style not unlike Classic ASP.

As a former ASP and ASP.NET developer, I took to this like Count Dracula to young ladies in nightwear.

Frankly, I sank my teeth in.

Now, let’s face it – PoSHServer is never going to be and enterprise-class, highly-performant, distributed web server in the same league as Apache or IIS, but what we’re looking at is a compact, flexible, lightweight, simple to run web server that Windows Ops guys can quickly set up and write code with, often simply re-purposing existing PowerShell scripts with just a couple of small tweaks. For instance, this deploy-on-merge hook solution uses a swathe of ready-made powershell to drive Octopus Deploy, and another about-to-be-blogged hook notifies us about AWS scale-up events and monitors cluster health while it does so.

You can install PoSHServer pretty simply, with a git pull and a scheduled task, set up with PowerShell, naturally. At this point, your PoSHServer is running and listening on whichever port you specified. Then you just drop a script in and get going. Our PoSHServer, for instance, talks between Octopus Deploy, Slack (incoming and outgoing), GitHub, Bitbucket and the AWS SDK, among others. It’s starting to become a key hub between our services, and is heading towards critical, monitorable and highly available territory**.

Unfortunately for us, some requirements popped up which weren’t – at first – accommodated by PoSHServer.

For one thing, in its original form, it would return you a 200, a 404 or nothing.

Not great, given that one of my use cases was a healthcheck endpoint that would return 200 OK when healthy and 500 when not.

So I forked the project and tweaked a bit of code to allow me to override HTTP Status Codes in script. I can now claim to be a teapot, if I should so choose.

Then I discovered that not only did I need to return 500 on error, but I also needed to return application/json, and PoSHServer defaults to text/html for PowerShell scripts.

So I changed it a bit more, and I sent a couple of pull requests in to have my changes merged into the original repo. I also found that a tweak was needed to cope with responses over about 29KB in size, so I added some .flush() calls and committed those too.

Hopefully, the PRs will be accepted and merged, but if they don’t, well, this is the joy of open source. I can maintain my fork as long as needed, tweak it as much as needed, and Domain’s PoSHServer will be just dandy, regardless of vendor support.

Which I think is pretty cool.

If you’re doing Windows DevOps, I recommend it. And please, contribute back. Make PoSHServer better for everyone. And have fun while you’re doing it.

 

* It runs PHP too, but let’s face it – PowerShell is it’s raison d’etre.
**  At which point we might have to port everything to C#, however I digress…

Leave a Reply

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