Chapter 8: Loops and Decision Making Constructs


Overview

Administrative scripting would be very limited if all you could do was execute a series of commands in a given order. The real benefit of administrative scripting comes from adding logic constructs to your scripts. In this chapter we'll explore how PowerShell uses loops and decision making constructs.

One advantage PowerShell has over the traditional cmd shell is that you can create loops and logic structures directly from the console. If you've read this book from the beginning, you've seen examples like this:

 PS C:\> foreach ($i in (1,2,3,4,5)) { >> write-host "Current value is "$i >> } >> Current value is  1 Current value is  2 Current value is  3 Current value is  4 Current value is  5 PS C:\> 

The brace ({) at the end of the first line tells PowerShell there is more to the ForEach command. When we press Enter, PowerShell changes the prompt to >>, which indicates it is waiting for the rest of command. Once we enter the last element and press Enter for a new line, the command is parsed and executed.

We mention this because many of our examples in this chapter are presented as PowerShell scripts. The benefit is that it is easier to edit and re-run a script than to retype an entire logic construct since that provides another opportunity for you to make a typing mistake.

Let's start our exploration with the logic construct we just used.



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