Posts

Showing posts from January, 2020

Testing Self-Contained PowerShell Script with Pester

In this post, I am going to share how to unit testing helper functions in a self-contained PowerShell script. I have the opportunity to write  AWS Systems Manager Command Documents that are actually PowerShell scripts. The SSM documents (PowerShell scripts) are then executed by on triggers so I rely less on the Windows Scheduled Task. This is one of the cases that I prefer managing code in a self-contained PowerShell script rather than modules. (Another one is PowerShell Lambda). Self-contained scripts are the scripts that contain helper functions and business logic codes in the same file. When evaluating the dependency management effort versus delivering results, I'm really less inclined to use config management tooling to configure my Windows worker server. Anyway, that's another topic for another post. <TL;DR> In the Pester test file, use Powershell Abstract Syntax Tree (AST) to parse out the helper functions from the main PowerShell script. Then, save each functio