Learning PowerShell – Lesson Seven

Share:
PowerShell

PowerShell Lesson Seven

By: Steven Aiello

Teach Your Self PowerShell – More About If, Elseif, and Else

More about PowerShell “If” Statements

In the last lesson we were introduced to a lot of very powerful new concepts:

Write-Host: printed information to the screen from our PowerShell script.

Clear: cleaned old output from scripts off our screen

If: operator to introduce logical conditions into our scripts

Objects: we snuck into the lesson to start getting you used to how PowerShell works with things is uses to hold data

In this lesson we will focus more on some of the further options that the if family of statements provides. If you didn’t feel quite comfortable with lesson six I would recommend you revisit it before moving onto this lesson.

I’m going to through a lot at you in this next image, but it is ok. Look at the picture and try not to think about what the code is doing try to understand logically what’s occurring in the script.

Everything up to line number 4 should look completely familiar. If your still confused by the simple if statement go back to lesson six and review that material, or post a comment in this blog post and I will try and expand on my previous explanation.

Everything new in this screen shot comes in at line number 6 and later. We have two new logic statements.

elseif and else

So what do they mean? Elseif can be a very simple concept. Think about a stop light in the United States. Whenever you approach a stop light you look at the light.

If(light_color equals red){

Stop

}elseif(light_color equals yellow){

Slow down and stop if it’s safe to do so

}else{

Keep driving through the light

}

In this case the else would be a green light. You could actually rearrange these in any order you like, you just have to evaluate the statements. So what’s going on? Much of what we do in day to day life corresponds to computing decision making.

The if statement expressly tested if the light_color was equal to red. If the color was red then preform the stop action

The elseif statement expressly tested if the light_color was equal to yellow. If the color was yellow then preform the slowdown and stop if it’s safe to do so function.

The else function grabs all remaining possibilities. In our cage the only remaining possibility is that the light_color will be green and the car should keep driving.

Once you start thinking about you make many many if, elseif, else actions in your life all the time.

If(I want to eat pizza equals true){

Go make or buy pizza

}elseif(I want to eat noodles equals true){

Cook some noodles

}else{

Just eat something

}

Writing scripts is really just an extension of what we already do in our day to day lives. How can we use this for writing a computer script? Let’s look at the image below

What do you see here? Have you ever observed the result “Maybe Running” as an output of the Get-Service command? No you haven’t! So what’s happening in the script here is that the if statements are going through their series of checks.

First let’s check for a status of “Running” which is valid

Second let’s use elseif to check for a status of “Stopped” which is valid

Third catch anything else and tell me there was an error

This concept is called error checking. We know what the script SHOULD return but we want to make sure that we catch everything else we don’t expect. Error checking is very important and you should always make sure you try and catch errors. A best practice is to explicitly check for conditions you know to be true and expect and then produce an error for all other states.

If, elseif, and else statements are really straight forward. If you use your imagination you can start to write from very powerful scripts just with the lessons you have learned so far.

 

Share:

Leave a reply

eight + = sixteen

This site uses Akismet to reduce spam. Learn how your comment data is processed.