Powershell tutorial 8 part 1
By now you should have a pretty good idea how powershell can be leveraged to help you with all sorts of tasks. This tutorial will cover the foreach loop to show you how you can chew through collections such as string arrays or windows services. There are two types of foreach loops in powershell, the foreach statement and the foreach-object cmdlet. They work similarly and you can get the same results with either, but they do differ in several important areas.
The foreach statement loops through elements in a collection such as a string array. The loops runs once for each element, executing a block of statements called a script block. To create a foreach loop you must define the collection that will be looped through, a variable to hold each element in that collection, and a script block that runs each time the collection is stepped through.
lets take a look;
$birds = “owl”,”crow”,”robin”,”wren”,”jay”
foreach ($bird in $birds)
{
“$bird = ” + $bird.length
}
in this example the first statement declares the $birds variable and initializes it with a string array that then uses the variable in a foreach statement. The foreach statement first begins with the foreach keyword, followed by a set of parentheses that enclose the three components ($bird in $birds). The first component of the string is the element variable which we defined to use with the element variable $bird. This variable can be named what ever you would like as long as you adhere to powershell’s naming convention. In short the element variable holds the current value as the statement loops through the collection. So in the prior example the $bird variable would be owl on the first pass, crow in the second pass, robin in the third and so on.
The second component in parentheses is the keyword “in”. This component is pretty self explanatory and should be used just as it is. The third element is the actual collection. In this case that collection is accessed through the $birds variable.
Next we see a series of brackets or braces. These brackets enclose the script block that is executed when the script runs. In this example the $bird variable retrieves the collection value, and the Length property retrieves the number of characters in that value. Even though this example only includes one statement in the script block, you can include as many statements as needed.
Lets look at another example.
$count = 0
$birds = “owl”, “crow”, “robin”,”wren”,”jay”
foreach ($bird in $birds)
{
$count += 1
“$bird = ” + $bird.length
write-host
}
“total number of birds is $count.”
The very first line of the script block increments the $count variable by 1. The second statement creates the string and sends it’s output to the console. The third statement is a write-host cmdlet, which only adds a blank line to the output. When each loop is run, all three statements in the script block are run, but the code after the script block is only run once after the last loop is complete. This code uses $count within the output text.
These examples have all assigned the collection (a string array) to a variable, but you don’t have to take this approach. You can define your collection directly within the foreach statement. Furthermore you can define a collection made up of other object types such as in the following example.
foreach ($svc in get-service)
{
$svc.name + “: ” +
$svc.canstop.tostring().toupper()
}
In this example the third component in parentheses is “Get-service”. This cmdlet returns a collection of objects. There is one object for each services on the local machine. A service object is assigned to the $svc variable each time through the loop. The foreach statement uses $svc to retrieve the service’s name and appends a colon. Next foreach uses $svc to access the service object’s CanStop property, which returns a Boolean value that specifies whether or not the service can be stopped once it has started. Lastly the foreach statement calls the tostring and toupper methods to format that value. the Canstop property value must first be converted to a string with the tostring method before uppercasing it with the toupper method. If you don’t want to convert the results then you will not need to include the tostring or toupper methods.
In the next tutorial we will discuss piplining with foreach statements and the differences between the foreach statement and cmdlet.
Loading ...



Discussion Area - Leave a Comment