First off, notice how I
initialize $a to the value of 0. This is because the first operation we do within the code
block is to increment its value by 1. The condition for the loop is to run when $a is less
451 Chapter 13: Windows PowerShell
than 11. The twist is this: if $a equals 3, you issue the continue statement that forces it
to jump through the next iteration of the loop and skip the rest of the code, including the
write-host cmdlet. If the value of $a is equal to 8, you instruct Windows PowerShell
to cease processing the code block immediately and jump out of the loop. This results in
this output:
Starting to count to 10??¦
1
2
4
5
6
7
Done!
Notice how it skipped outputting the number 3 since the continue statement forced
the code to the next cycle and the counting stopped at 7 instead of 10 since the break
statement took effect when $a was equal to 8 but before write-host got a chance to
output its value.
POWERSHELL IN ACTION
If you??™re like most Windows systems administrators, you want proof that PowerShell
can make your life easier before you commit to using it. The good news is that using just
the basics covered so far, you can immediately perform a large number of interesting
real-world tasks.
Let??™s go back to the Get-Service cmdlet. Get-Service returns a collection of
service objects including their various properties. Suppose you want to show a list of
services that are currently running based on the data retrieved by Get-Service.
Pages:
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482