How To Use PowerShell’s If-Then-ElseIf-Else Construct
Think you can do better? Show us!
In this video, Adam demonstrates how to use PowerShell’s if/elseif/else construct. He’ll cover a scenario where some servers are online while some servers are offline to only perform an action on the available ones. If statements are all about defining conditions ahead of time and this is a great example of that.
In this snip We are going to focus on Powershell's, if elseif and else constructs to demonstrate this we're going to read a file on a set of servers or at least attempt to and then validate if their online or not if not, we're going to return an error. If so, we're going to go ahead and just read the file. We can do this with anything, it's just a really good way to define all these different conditions that we could run into. Alright so to get started. I'm going to use a simple loop. So I have an array of servers here just array of servers. SRV one localhost, which it was just my local machine here and server 3 and then I'm just going to run through all those I'm going to iterate over each of those, and just try to read a file called some file dot texts on the root of C so we have no conditional logic at this point. So when I run that notice that we are returned a few errors here, so cannot find path. SRV one some file. We got Foo and bar which is the actual contents of some file and then we have cannot find path some file dot text on SRV 3. There's no conditions here. We didn't account for any kind of environmental changes so could be the server could be offline that file does not exist could be in there could be a lot of different conditions. We need to test and perform an action based on whether or not certain. Conditions are true or not. So to demonstrate that maybe I want to check and see if the server is online first before I do it. This is really good error handling so to do that. I have a test connection here and I just wanted. This is just going to ping one server. For now when you're doing. These things we need to create the expression first figure out the syntax on the expression so notice that test connection returns false when I run this again on one that returns true that means it's online so I know that based on prior experience. If the server is online that's good that will evolve us to the next kind of test which then. If the server is online then maybe the file exists. But if the server is not online then we know that we cannot access the file on that server obviously. Alright so now we want to add an if statement here. Now I have here in line 20. The syntax for an if statement. We have if and then we have open paren. close paren and then there's an expression in between the expression is that test connection sip it in this case, and then inside of that inside of the script block with the left curly brace and close curly brace there. We have any kind of code that you want if the expression evaluated be true now in an if statement evaluate to be true, it can evaluate to be not null or can be evaluate anything other than. Zero so there's 3 different ways that the if statement will evaluate to be true. And if statement always evaluates a boolean true or false one or zero so you can remember that. So taking this a step further. Now I'm going to iterate over each of these and then ping each server. I'm going to do. If this expression is true, then try to run the get contents of the file. Now notice that we didn't return any errors whatsoever and we did get foo bar, which is the contents of one of those files. We were able to account for all those errors ahead of time, but we accounted for one condition this time, the condition of the server is online. But we can take this a step further and count for more than one condition. So I am looping over each of the servers testing to make sure that it's online or not, and if it is online. Then it's going to grab the content same thing, but notice there that I have an else statement as well, I can use else, I can tag. Else left curly brace right curly brace there and put some kind of code. In between there and what that's going to do is that's going to ping. The server if the server pings is going to try to get the contents of the file. Else otherwise any other condition whatsoever. It just if test connection doesn't return true. If it returns. Zero null or boolean false then it will go ahead and run the right error statement here. So when I run that you'll see that it goes through each of those. The server serve. One is not responding then we got foo bar then we go to the server SRV 3 is not responding see we accounted for 2 different conditions one the server was online and the server was offline. We did something depending on each of those conditions. And finally we can take this a step further by adding one or more unlimited else if statements. We can add a bunch of else ifs. You don't want to add a ton of these else ifs at that point I will get into switch statements which there's another there's going to be another step for at a later time. But in this instance here. I'm kind of switching this on its head a little bit so notice that on 43. I'm saying if the I have that code expression there. and I'm saying if the server does not ping I'm flipping the bit there. I'm just saying if the server is not online by using the not operator if it's not online. Then I'm going to write an error. The server is not responding and then I have another condition accounting for another to condition with else. If I can just add on the tack on these as I need to else if. The some file dot text does not exist. I'm accounting for that condition as well, and then if that condition returns. True then I'm saying write the error, the file does not exist on server. Whatever the server is this time, else Then it's going to finally get it. So my script is evolving here. I'm accounting for all these different conditions by Tacking on all these different else if else. So I'm able to make the script much more dynamic this way, much more flexible and this is really good error handling ability. So let's go ahead and run this. OK, so now you can see that it says the server SRV One is not responding because it's not online. It got caught by the condition on Line 43, then we say Foo Bar, which is the contents of the some file dot text. That means that line 48 ran because obviously it went down through each of those conditions. It was online and the file was available. It did check check check and then it ran get content and then finally the last the server SRV 3 is not responding. As well all right, so now let's try to mimic let's try to get line 46 to run. We didn't have that to run, so to do that. I'm going to go ahead and just delete this file completely. And go ahead and run this again and let's see if we can get each of those conditions to apply. Alright so now it looks like we did here so it says. First, one, the server SRV one is not responding. We're able to account for the first condition on line 44. Then the file does not exist on server localhost notice that we have a condition for Line 46 and then server 3 is not responding because that's the headline 44 because server 3? What does not exist, obviously so in this case see that it's flexible it counts for all these different conditions. We can as long as your if statement returns a true a one or not null. It's going to return. True value so then it's going to run whatever is in the next step. So this has been a quick demonstration of how the powershell if elseif and else construct works thanks for watching.