Testing PowerShell with Pester

Posted on August 12, 2016 by Jeff Hicks in PowerShell with 0 Comments

PowerShell-Text-Purple-hero

If you are an experienced PowerShell user, chances are you have heard of Pester. This is an open source project that Microsoft started shipping as part of Windows 10. I’m not going to try and teach Pester here, although it really isn’t that difficult to pick up. But I wanted to show you some ways to use Pester that you might not have considered.

Pester is typically designed for software testing. You build a test script to run through different parts of your code and Pester validates it. This is a quick way to verify you haven’t broken something while introducing something new.

A traditional Pester test

A traditional Pester test (Image Credit: Jeff Hicks)

But there’s no reason we can’t use the Pester logic to test other things. Perhaps that status of a critical server. The centerpiece of Pester is a logical test of “If some condition meets some test it should be some value”. It’s not that difficult to write a test that says “the DNS service should be running.” Here’s a simple Pester test to validate the state of my primary Hyper-V server.

I could run this as a regular PowerShell script. But I prefer to use the Invoke-Pester cmdlet.

Invoking a Pester test script

Invoking a Pester test script (Image Credit: Jeff Hicks)

By using Invoke-Pester I can pass the results to the pipeline, output the results to XML and even specify what tests to run if I’ve named any of my tests. The benefit of using Pester is that you can automate the process running the test and taking action should there be any failures.

To test this, I’ll modify one of my tests so that it will result in a failure.

A Pester Test Failure

A Pester test failure (Image Credit: Jeff Hicks)

The failure is pretty easy to pick out. I also used the -Passthru parameter so you can see what kind of output to expect. I can then automate code like this which will email me failures.

Failure email notice

Failure email notice (Image Credit: Jeff Hicks)

I think that’s pretty slick. But it gets even better.

Since we’re talking PowerShell, you can use it to dynamically build your Pester tests. Here’s a test file that dynamically generates the same tests but with different expectations per server.

I’ve hard coded the input values, but you could just as easily input them from an external source such as XML.

Testing server infrastructure with Pester

Testing server infrastructure with Pester (Image Credit: Jeff Hicks)

And, of course, I could build a response script to take remedial action for failures. Depending on what you are testing, if you configured the server with DSC, you have similar testing options. Although with Pester I can test for more intangible items like free disk space or free memory.

Sponsored

Or, here’s a simple Pester file for testing Active Directory and my domain controllers.

Testing Active Directory with Pester

Testing Active Directory with Pester (Image Credit: Jeff Hicks)

As you can see, I have some disk space issues to sort out.

Sponsored

Pester is a skill I think every PowerShell professional needs to begin developing. Start with simple tests for your modules. Once you gain a better understanding of how to construct effective tests, you’ll realize there are many things you can test and your investment in learning PowerShell continues to pay off.

Sponsored

Tagged with