Manage Learn to apply best practices and optimize your operations.

A Pester test takes the guesswork out of PowerShell scripts

No one wants to write a PowerShell script only to discover later that it doesn't work as anticipated. Pester provides a testing language to ensure code lives up to expectations.

Most IT professionals who have written a script know it's difficult to create code that works flawlessly. Even...

the most skilled programmers cannot account for everything that happens when a script runs.

One way to eliminate the guesswork with scripts is to use Pester. This open source unit testing framework written in PowerShell enables developers and administrators to build tests using a domain-specific language. Run a Pester test to verify that your commands -- whether from a PowerShell script, Desired State Configuration resource or Chef recipe -- were followed. The service then shows you what outcome you can expect.

Admins traditionally use Pester for unit testing, or code testing, but it does much more. Pester enables you to embed PowerShell code inside of tests. Admins can use a Pester PowerShell test to confirm the infrastructure is in the state they expected.

Confirm code integrity with a Pester test

If you use a script or some other external process to create an Active Directory user, confirm the user has a few attributes, such as a first name, last name, department and title. Invoke a Pester test to verify these attributes after the script or process runs.

A Pester test consists of a PowerShell script written with one or more Describe blocks, each of which contain one or more It blocks. Each of these It blocks includes a single test, which could look like the following:

New-ActiveDirectoryUser.Tests.ps1
==============================
describe 'AD User Creation' {
    it 'creates an AD user with the appropriate attributes' { 
    }
}

This is the structure of a basic Pester test suite. Once you build the structure, fill in the validation code, which is the code required to determine if what you're testing performs as intended. In this case, we invoked a PowerShell script called New-ActiveDirectoryUser.ps1 to create an AD user that looks like this:

New-ActiveDirectoryUser.ps1
==========================
New-ADUser -Name 'bjones' -givenName 'Bob' -surName 'Jones' -Department 'Accounting' -Title 'Manager of Accounting'

For the test, invoke the script to create the user. Then build some code to validate that the script created the user. To do this, run some code to read whatever was just done and use one or more assertions against a set of expected values.

New-ActiveDirectoryUser.Tests.ps1
==============================
describe 'AD User Creation' {    
    ## Invoke the script
    & .\New-ActiveDirectoryUser.ps1     
    it 'creates an AD user with the appropriate attributes' {
          $user = Get-AdUser -Filter "name -eq 'bjones'" -Properties Department,Title 
          $user.givenName | should be 'Bob'
          $user.surName | should be 'Jones'
          $user.Department | should be 'Accounting'
          $user.Title | should be 'Accounting Manager'
    }
}

The should be references are called assertions and are part of Pester, not PowerShell. Assertions compare an actual state against an expected state.

Once you build the test, use the Invoke-Pester command to run it:

Invoke-Pester C:\New-ActiveDirectoryUser.Tests.ps1

Pester test
Figure 1. In this Pester test, the code fails because the expected title, Manager of Accounting, does not match the result, Accounting Manager.

The Pester test flagged the title because the text was wrong. Fix the script, manually correct the user account in AD and then try again.

Pester test without errors
Figure 2. A Pester test without errors will return results with green text.

The green output indicates that the Pester test didn't find any errors.

Imagine all the possibilities with Pester -- you can build a Pester test to confirm your infrastructure meets expectations. Pester also enables administrators to keep tabs on the code used to make changes as well as the infrastructure itself.

Pester is installed by default on Windows 10 clients and higher. Download the Pester PowerShell module via the Github project or via Install-Module Pester.

Next Steps

Pester your PowerShell scripts until they work

WMF 5.1 delivers advanced PowerShell cmdlets for admins

Common PowerShell commands and when to use them

This was last published in June 2017

Dig Deeper on Windows PowerShell Scripting

PRO+

Content

Find more PRO+ content and other member only offers, here.

Join the conversation

2 comments

Send me notifications when other members comment.

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Please create a username to comment.

What tools do you use to test scripts before they run?
Cancel
I just tried this out. FANTASTIC! Thanks for sharing. Turns out that PESTER already existed on my machine and I did not know it. Thanks.
George
Cancel

-ADS BY GOOGLE

SearchServerVirtualization

SearchCloudComputing

SearchExchange

SearchSQLServer

SearchEnterpriseDesktop

SearchVirtualDesktop

Close