Month: February 2016

Powershell–Break Outta Loops!


Going back to the basics…

Sometimes it’s good to pay homage to the simple things in programming. Today, we’re gonna do a lite surface scratch on while($true) loops and the break statement. This came about as I was adding some retry logic to one of my functions. I wrote several lines of very ugly code including several nested try { } catch { } blocks. Ugh. It was then I stepped back and thought, this is a perfect opportunity for a while($true) loop! These little guys are awesome but I always hesitate a bit thinking about putting my function into a never-ending loop. But with the help of a few well placed break statements, it’s the cleanest way to check the result of something and retry if it’s not what you expected.

Let’s take a look…

Just a simple while loop. By setting a variable $var before the while($true) loop and incrementing it inside the loop, we can count the loop iterations. With a simple break statement, we can break out of the never-ending while loop.



The beginning

This is the story of how I started to love Ruby.

The Setup

I was working on adding integration tests to one of our main chef cookbooks, the one that installs and manages our SQL Server instances. Being a Windows shop, I used Pester and the Kitchen-Pester plugin.  I had a working set of Pester tests that verified all of the configuration settings on the server. Test-Kitchen passed when I ran the steps individually: