PowerShell 105 – Looping

It’s been a while but I found some time to write another blogpost in my PowerShell 10x series. During this PowerShell 105 we’re going to take a closer look at loops. I love loops. They’re a quick and easy way to iterate through an array or keep doing stuff until …. something. I’ll handle “While” and “Foreach” loops. I already used these in the PowerShell 102 blogpost. These loops become very interesting when you combine them with operators (discussed in previous post).

Looping

Performing action “While” something is valid

“While” is interesting to use when you want to keep retrying an action until it reaches a certain point. Recently I wrote a script that performs actions remotely and rebooted the machine a couple of times. So after each reboot I needed to wait and check when it was back online. More importantly when Remote PowerShell was back up and running.

So how can we check if Remote PowerShell is up and running. That’s fairly easy, you see if you can retrieve the machinename. After that you compare the machine you’re trying to reach with the machinename you got back. If that’s returns “True” you’re good.

So how do we actually get this in a “While” loop so it keeps executing until the machine is reachable using Remote PowerShell? Setting up a basic while loop is simple:

Each time the loop starts we check if the variables do NOT match (-ne = not equals). So inside the loop we actually have to do the magic.

So what we do is we wait until $MachineOnline has the same contents as $Machine. Inside the loop we try and retrieve $MachineOnline. I’ve added  -ErrorAction SilentlyContinue  because we’re expecting it to fail at least a couple of times. Without this your script will terminate because the cmdlet fails. I also added a sleep timer in there because it makes no sense to check the machine that often. A check once every 5 seconds is enough.

 

Doing something “foreach” item in a collection

My usual suspect for doing a foreach is having to do one or more actions on a list of servers. So I’ll use that as an example.

First thing I do is I get a list of machines from Active Directory. You can replace this with any piece of code. As long as you load an array (or something similar which foreach can iterate through) into the  $Servers  variable you are good to go.

Now we actually get to the foreach. So you state the foreach loop and tell PowerShell that you want to run through $Servers while $Server will be the current item in the iteration. So in this case I’m retrieving the serial number using  Invoke-Command  and the server model using  Get-WmiObject . Finally I add both values and the server name to the  $Output variable. You can use this output variable to export to file, write to screen or do anything else with it you might want to.

Conclusion

Both items are a must have for anyone who uses PowerShell. I’ve just demonstrated a single use for both methods but you can become creative and use them in totally different scenario’s.

PowerShell

Leave a Reply