For


The For loop is similar to the ForEach loop. With this statement we can keep looping while some condition is met and execute a block of code each time through the loop. The condition could be a counter. For example, we might need a loop that says start counting at one and execute some block of code each time until you reach ten. Or we might need a loop that says as long as some statement is true, keep looping and execute a block of code each time.

If you've used the For loop in VBScript, conceptually PowerShell's implementation is no different. However, the syntax of PowerShell's For loop may confuse you at first:

 for (<init>; <condition>; <repeat>) {<command block>} 

This syntax essentially instructs PowerShell that for (some set of conditions) {do this block of commands}.

Let's break this down. The <init> element is one or more sets of commands that are separated by commas. These commands are run before the loop begins. Typically this is where you initialize a variable with some starting value. This variable is usually checked by some statement or code, <condition>, that returns a Boolean value of TRUE or FALSE. If the condition is TRUE, then the code in the command block code that is enclosed in braces, <command block>, is executed. The <repeat> element is one or more sets of commands that are separated by commas, and are run each time through the loop.

Traditionally these commands are used to modify the init variable. Each element in parentheses is separated by a semicolon or a carriage return. Thus, you could have a For statement that looks like this:

 for (<init>     <condition>     <repeat>){     <command_block>     } 

Here's a very basic example:

 PS C:\> for ($i=1;$i -le 10;$i++) {write-host "loop #"$i} loop # 1 loop # 2 loop # 3 loop # 4 loop # 5 loop # 6 loop # 7 loop # 8 loop # 9 loop # 10 PS C:\> 

The initial command sets $i to a value of 1. The condition that is checked each time is to see if $i is less than 10. If it is, then we use the Write-Host cmdlet to display a message. Each time the loop is executed, $i is incremented by 1 by using $i++.

This is a very complete example. However, it's possible to reference other variables from within the same scope. The following command is essentially the same, except $i is defined outside of the For statement.

 PS C:\> $i=1 PS C:\> for  (;$i -le 10;$i++)  {write-host "loop #"$i} loop # 1 loop # 2 loop # 3 loop # 4 loop # 5 loop # 6 loop # 7 loop # 8 loop # 9 loop # 10 PS C:\> 

Notice the For statement's condition, which is the portion in parentheses, has an empty init value. Even so, we still include the semicolon delimiter.

Runaway Loop

Be careful with the For syntax. If you do not properly specify an expression to evaluate each time through the loop, it will run an infinite number of times until you press Ctrl-Break or kill the PowerShell process.



Windows PowerShell. TFM
Internet Forensics
ISBN: 982131445
EAN: 2147483647
Year: 2004
Pages: 289

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net