Loop through a collection of items with the Pester TestCases parameter instead of using a foreach loop

One of the huge benefits of attending in-person technology events is getting to network with others. While at the MVP Summit last month I had a chance to demonstrate some of my PowerShell code and Pester tests to Jim Truher.  I was developing the code and tests for a session to present for the PowerShell Virtual Chapter of SQL PASS (the code and a video of that presentation can be found here).

In one of my examples, I was looping through a collection of items (computer names of SQL Servers) to run Pester tests against each one using a foreach loop similar to what you’ll find in this blog article. Jim wrote the following code (on my computer) to demonstrate how the Pester TestCases parameter could be used to loop through a collection of items without the need for a foreach loop:

testcases1a

He also demonstrated a failing test:

testcases2a

His examples could easily be modified to perform validation of multiple computers (SQL Servers in the following example):

testcases3a

Thanks to Jim, I now know of a new more efficient way of looping through a collection of items for Pester tests.

µ

1 Comment

  1. svangulick

    I wasn’t expecting to be able to use a param block in a ‘it’ block. This is really great.
    Thanks for sharing!!

    Reply

Leave a Reply

%d bloggers like this: