官术网_书友最值得收藏!

Using loops

In this section, we will review the usage of loops in Windows PowerShell. Every scripting language has multiple ways to loop through an array of items and execute a block of statements. Windows PowerShell has a variety of looping mechanisms described next.

The Do loop

Here is another way to run a statement block based on a conditional result. The Do loop is used with While and Until keywords to run a script block along with a conditional statement. Both the loop Do-While and Do-Until run once before the condition is evaluated. The command block is executed in a Do-While loop as long as the condition is true, whereas a Do-Until loop runs until a condition evaluates to be true.

The syntax of Do-While and Do-Until is the same. The following shows the syntax of the Do-While statement:

          do {<command block>} while (<condition>)
          do {<command block>} until (<condition>)

Examples are as follows:

Count to 5—In the following example, the statement is to use an increment operator with a $a variable and increment the value by 1 (as listed earlier in the assignment operators section of this chapter), and the condition is evaluated to be true until the value of a is less than 5. Here is how we will use the loop in a single line in PowerShell:

PS C:\> Do { $a++ ; Write-Host $a } while($a -ne 5)

The previous statement can be used in a different way where a carriage return is used instead of a semi-colon:

 PS C:\> Do { $a++ 
 Write-Host $a
 } while($a -ne 5) 

The Do-Until loop executes the statement list until the condition is false. Once the condition evaluates to be true, the loop exits.

In the following example, the script block will write the output of the variable to the console and increment it by 1, and the process is repeated until the condition, which is the value, becomes greater than 5 is evaluated to be false. Once $Count exceeds number 5, changing the condition to true, the loop exits:

PS C:\> $Count = 1
do {Write-Host $Count; $Count++}
until ($Count -gt 5)

ForEach loops

The ForEach statement is used to iterate through a series of items in a collection. A typical collection is to traverse through the items in an array. You can specify within the ForEach loop a command or a command block against each item in the array:

Syntax is as follows:

                     foreach ($<Element> in $< group of items>){Command Block>}

The following ForEach loop displays the values of the $Number array.

In Windows PowerShell, the entire ForEach statement should be in one line for execution. Here is an example:

$Numbers = "1","2","3","4","5"; foreach ($No in $Numbers) {Write-Host $No}

If you are using a PowerShell script file, you can use the ForEach statement using multiple lines in a .ps1 file:

  $Numbers = "1","2","3","4","5"
  foreach ($No in $Numbers)
  {
  Write-Host $No
  }

In the previous example, the $Numbers array is used to store values from 1 to 5. The ForEach statement loops through each of the items in the $Number array. In the first time, it sets the $No variable with value 1, second time with value 2, and so on. The loops exit when the value of 5 is reached.

We will use ForEach in the remaining chapters with cmdlets that return a collection. For example, we will use ForEach to iterate through each of the file/directory returned by Get-ChildItem in this case:

foreach ($item in Get-ChildItem)
        {
              Write-Host $item
        }

Loop through an array of strings:

 $Fruit = @("Apple","Peach","Banana","Strawberry") foreach ($fruit in $fruit) { "$fruit = " + $fruit.length }
The following example displays all the numbers in the series except 2:
foreach ($num in 1,2,3,4,5) { if ($num -eq 2) { continue } ; $num }

While loops

The While loop is used to execute statements in the command block as long as the condition is evaluated to be true.

Here is the syntax of a While loop:

while (<condition to be evaluated>) {<Command Block>}

In a while statement, the condition is evaluated to be either true or false. As long as the condition is true, the command block is executed, which can be a combination of more commands running through each iteration of the loop.

The following example lists numbers from 1 to 9 as the value of variable is $value is evaluated to be true until its value is equal to 9:

  while ($value -ne 9)
  {
  $value++
  Write-Host $value
  }

You can write the same command in PowerShell in one line as follows:

while($value -ne 9){$value++; Write-Host $value}

Note that there is a semi-colon (;) that separates the two commands in the command block. The first one increments the value of $value by 1, and the second command writes the output of $value to the console.

主站蜘蛛池模板: 广宁县| 肇州县| 卢湾区| 铜陵市| 甘谷县| 张掖市| 靖安县| 金山区| 九寨沟县| 永泰县| 墨竹工卡县| 云南省| 桐庐县| 永川市| 山西省| 平阴县| 韩城市| 靖安县| 双城市| 道孚县| 乌恰县| 乾安县| 启东市| 昌图县| 双鸭山市| 武城县| 崇仁县| 清丰县| 咸丰县| 新野县| 祁门县| 长治县| 门源| 城固县| 青河县| 揭东县| 东乡县| 泰宁县| 确山县| 巍山| 枣阳市|