Summary: Microsoft Scripting Guy, Ed Wilson, talks about using the Do…While statement in Windows PowerShell scripts.
Microsoft Scripting Guy, Ed Wilson, is here. Last week, the Scripting Wife and I were at the Windows PowerShell Summit in Bellevue, Washington. It was an awesome week. We got to reconnect with many old friends, and we also made new ones. I delivered three sessions on Wednesday. Needless to say, I was a bit tired. In fact, the entire week was tiring. I am talking about 16-hour days, and a three-hour time zone change kind of tired. But the energy was buzzing around; and therefore, it was awesome.
Before one of my sessions, I decided to get a cup of coffee. Someone saw me and said, “What are you doing with coffee? Don’t you usually drink tea?” Well, yes I do, but I thought I would go for the "hard stuff" to provide a little extra jolt before my session.
After one of my sessions, someone came up to me and said they liked my blog posts last week, and wondered if I would write something about Do. A "Scooby Do, where are you" kind of do? Probably not. Here you go...
The Do…While loop construction in Windows PowerShell is a bottom evaluated version of the While loop that I talked about in PowerShell Looping: Using While. The whole idea is that I am going to do something while a condition exists. As an example, here is a recent weather forecast for Charlotte, North Carolina, where the Scripting Wife and I live:
So I might go outside and ride my bike while the sun is shining. The strange thing is the way the evaluation goes. It might look like this:
Go to the garage.
Get the bike.
Ride while it is sunny.
The evaluation occurs at the end. In Windows PowerShell script, it would look like the following:
Go to garage
Get the bike
} (While (weather –eq ‘sunny’)
Now I want to take a look at a real Windows PowerShell script that illustrates using the Do…While loop.
In the script that follows, I set the value of the $a variable to equal 1. In the Do loop, I print a message that I am starting the loop. Next I display the value of $a, and then I increment $a. I print the current value of $a, and then close the script block. In the While evaluation block, I check to see if $a is less than or equal to 5. The script is shown here:
$a = 1
"Starting Loop $a"
"Now `$a is $a"
} While ($a -le 5)
The script in the ISE and the output from the script are shown in the following image:
So, I can see that there are four parts to a Do…While loop. First is the Do keyword, then the script block that I want to “do.” Then comes the While keyword, and the condition that is evaluated to determine if another loop will occur.
In many respects, the While statement and the Do…While loop are similar. The difference is that with the While statement, the condition is evaluated to determine if the script block will take place. In the Do…While loop, because the condition occurs at the end of the loop, it will always loop at least once.
This is illustrated in the image that follows. I use exactly the same script as I used previously, but instead of setting the value of $a, I assign a value of 9. Now, 9 is greater than 5, so the ($a –le 5) condition is never true. Yet, as you can see in the image, the script block ran once.
A more practical example
I decided to write a little script that will monitor for the existence of a process by name. As long as the process runs, the script continues to loop around. To ensure that the process runs, I start it at the beginning of the script. I then use the –contains operator to look for the process by name in the control portion of the loop. Here is the script:
$p = "notepad"
"$p found at $(get-date)"
$proc = Get-Process
} While ($proc.name -contains 'notepad')
In the following image, I see that it took me a few seconds to close Notepad. When I manually close Notepad, the looping stops.
Windows PowerShell Fundamentals Week will continue tomorrow when I will continue talking about looping.
I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at firstname.lastname@example.org, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy