Understanding Do, Do/While, While And Do/Until Loops In PowerShell
Think you can do better? Show us!
Adam gets loopy in this video! He will cover all of the Do loops in PowerShell and attempt to explain the small nuances that make each one just a little different,
Hello in this snip. I want to talk about do, do while while and do until loops so we're going to just get loopy. That was that was a terrible terrible joke, but anyway, we're going to get loopy. In this snip first thing I want to talk about is do loops so do loops as my comments. Most helpfully suggest here is they always perform an action once and check for conditions last so to give an example of that, so let's say that we have a service called problem service. We have one of those infamous services on one of those servers that for whatever reason, this random. Piece of software has a service that has to be up, but always constantly crashes and we've decided to have a script setup that would constantly monitor this thing and we can do this by using a do loop. So the do loop tries to first start the service so regardless if the service is stopped. It started whatever it's always going to run the line 5 there first before anything. And then once it does, that the first time on line 6 there. It's in going to be do this. Other expression the while loop here, so it's actually going to run get service again check at the status is equal to stopped or not so it's going to do this over and over and over again. While the service is stopped so it's a really good real world example of how this works. So the do loop always runs a command first so let me actually put a real world service in here. If I replace this, I believe this is a real service, so let's do this. Remove this out of here. In first of all let's just see if this runs. OK, yes, so the WUAUSERV service is a real service on this machine. Let's see the status of it now, it is running OK. So when I run. This this should try to start it. It's probably it's already going to be started and then after it tries to start it. It's going to check and see if it stopped or not, and if it is stopped. It's going to continue so. It was real quick because it tried to start it. It was already started and then hit the while loop. He ran it check the status to see if it was stopped if it was stopped. It would have ran start service again. But it wasn't. It's very quick so a do loop is good for whenever you want to run an action once at least next. One is a while loop a while loop is similar, but you can see here from the comments. It does check the condition first and then runs the action last so again. Let's change this to a real service here so I will. Change problem service remove all this stuff out of here. OK and now you notice that it's flipped the condition to actually check to continue with the loop is up here is online 12 versus on line 6 up there so the while loop checks. The condition first and only if that condition is true, it will then run. These start service. So I will run this again notice. It's very fast because the it didn't actually get down to line 13 because it didn't need to be started the condition was checked ahead of time. So let's say that I go over here and change this to started but what do you think is going to happen now? I think it's probably going to just loop forever? It didn't so why didn't loop forever so let's see will do some impromptu troubleshooting here so we try this out, we check the condition. The condition is false. It says it's not started so let's see what the status is status. Actually, running OK, so let's change this to running. I bet now, it's going to loop forever. So now it's going to loop forever because the condition is running. It's going to check the condition. The condition was true, it's going to try to run tried around try to run and it's just going to run forever until it crashes my computer so. That is the while loop the while loop checks. The condition first and then does the action next and then finally we have the do until this one is similar but it's kind of opposite of the do while loop so the do until loop runs the condition first all the do loops run the condition first so I will change this to my WUAUSERV again here, so we can have a real example. And then this does something until something else is true so this actually is different so notice that we have on line 6. We have this condition is while the services stopped and on line 22 it does exactly the same thing logically. This is kind of where you get into some Boolean algebra sort of stuff to where until and while are 2 completely different words if You think about it while you're doing something. Something happens until that kind of says I want to do this until something ends so they're different so in this instance here on line 21 through 22. This does exactly the same thing because the while and until are completely opposite. It's doing the same thing because I have an inverted because in line 22 there. I'm checking the status of running versus online 6. I'm checking the status of stopped same thing. This would be completely opposite. It's kind of it's a little unintuitive. At first if you're not used to it, but notice that that will just run forever. Because these services actually started and it's trying to go until it stopped. This would be fine if I had this to stop service here, so let's actually figure out what's going to happen here. We change this to stop now. It actually did stop the service when I check the service. Now it stopped so notice that it's kind of flipped on its head that do until and the do while are opposites of each other. That was an introduction to do, do while, while and do until loops, so we got loopy. Hope you're still OK. Thanks for watching.