Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


Share this Page URL
Help

PowerShell Basics > PowerShell Basics - Pg. 28

28 CHAPTER 1 Introduction to command shell scripting FIGURE 1.13 Output from the Switch Example against each case in the switch. This is actually handy, since, as we can see in Figure 1.13, some processes do have more than one instance running. Looping There are a number of looping structures we can use in PowerShell and we will look at a couple of the more common and useful possibilities here. Looping in PowerShell follows much the same format as looping in most programming languages. One of the simplest looping constructs we can look at is the for loop: for ($counter=0; $counter -lt 10; $counter++) { $ping = New-Object System.Net.NetworkInformation.Ping $ping.Send($args[0]) Start-Sleep -Second 5 } We would want to run this script with .\looping.ps1 codingforpentesters.com . Let's have a look at what we did here. First we set up the beginning of our for loop, for ($counter=0; counter elt 10; $counter++) . So, this line initializes $counter with zero. This will be the variable that keeps track of how many times our for loop has executed. Next, we evaluate $counter to make sure it is still less than 10 with elt . If this is true, we will continue; if not, we will stop right here.