PowerShell and ...Web Development?

Being on a team of two responsible for anything even tangentially related to technology, I've ended up wearing more hats than I ever expected I would. Thankfully, my career has been pretty consistently Windows-centric so I have had opportunity to learn and fall in love with PowerShell as a scripting and automation language... which, side note, is why I use cmdlette as my online moniker on GitHub, Twitter, etc. PowerShell scripts I've written have performed all sorts of tasks from making full vhd backups of virtual machines on Hyper-V servers to scraping event logs for security events. Recently, I've been using PowerShell as a pocket-sized web developer. Sure when all you have is a hammer everything looks like a nail, but PowerShell is much more like a hammer with a multitool built into the handle.

PowerShell and HTML

One hat I've had to wear being on a tiny team now is that of web developer. Thankfully, I wasn't tasked with anything too complex--just a basic, lightweight intranet site that I could build using only HTML and CSS. Most of the pages require zero upkeep on my part thanks to using embeds of documents that other people update, but one embed in particular is specifically my problem--the employee directory. 

We had been using a VB script that Justin found on the internet some time ago that wrapped HTML around Active Directory attributes to create an employee directory page. However, as someone who is capable of learning new things as I get older, I have zero appreciation for the infinity-pool of vomit that is VB. I was determined to replace the bulky, ugly VB script with a much lighter-weight PowerShell version... and after a little bit of time spent playing with the Get-ADUser cmdlet, and remembering where in the HTML portions I needed to use escape characters, I accomplished exactly what I set out to do.

The VB script I set out to replace was 378 lines of unintelligible garbage soup. Comments in the VB script weren't helpful, and didn't tell me which blocks of the script did what (yet another reason I hate VB is that VB developers are fucking sloppy--almost as a rule). I knew I could do so much better with PowerShell, and set a goal for my script to be no longer than 25% of the VB script. In the end, including comments, my PowerShell script came out to 44 lines (11.6% the length of the VB script) and produced a much nicer-looking directory alphabetized by last name. Of course, I needed to make the basic HTML pretty somehow, so I slapped together 32 lines of CSS (bringing the total to a mere 20% of the VB script). Here's the PowerShell script in all its dark mode Notepad++ glory, for reference:

This particular script kind of looks like variable hell, I know. I won't claim to be the mistress of all things PowerShell, but I made everything as clean and as readable as I possibly could. To break it down: We have a couple of different user Organizational Units, so I set the searchbase to only look at a specific OU containing real people and not any service or built-in accounts. We also keep the description attribute standardized for employees, which I used to filter out any of my test users lumped in with real people. From there, the most difficult thing was searching up how to turn the hex values of the images into... actual images. After I successfully got images to convert, it was easy to populate the rest of the fields I needed. I'm sure I could have split off a few more lines for better readability, but I was mostly after getting something to "just work" any time we need to add or remove someone to/from the directory.

I'm pretty satisfied with this round of code golf. Even including the CSS, I'm well under par compared to the dirty-bathwater-garlging Nurgle-like behemoth of the VB script I replaced. Now I don't need to worry about maintaining or understanding the old, messy VB script intelligible only to its original author. PowerShell gets the job done quickly and cleanly, and takes the weight of updating the employee directory off my shoulders. (Not to mention, it runs much faster than the VB script ever did.) I think my love for PowerShell is well-warranted, especially considering I can use it in unconventional ways like this.

You can find the sanitized script and the CSS on GitHub. I have more PowerShell to share soon, so stay tuned!

Comments

  1. Very well written article. It was an awesome article to read. Complete rich content and fully informative. I totally Loved it. Read more info about Web designer

    ReplyDelete

Post a Comment

"What you type into a comment box on the Internet
echoes in eternity."
- Gerald Ford