$ErrorActionPreference = "SilentlyContinue" $input1,$input2 = $null # START | The script asks for an IP, put it in a while for error checking. It only checks if the IP is a valid one. while ($input1 -eq $null) { [ipaddress]$input1 = read-host "Please input the first IP address you would like to scan: " } # END | The script asks for an IP, put it in a while for error checking. It only checks if the IP is a valid one. while ($input2 -eq $null) { [ipaddress]$input2 = read-host "Please input the IP address you want to finish the scan: " } # Grabs each respective input and puts them into their own arrays. [array]$ip1 = $input1 -split ".",0,"simplematch"; [array]$ip2 = $input2 -split ".",0,"simplematch" # Begin madness. The ping sweep is four nested for loops. Each for loop interates for each octet. We call the count for each octet a.b.c.d respectively. # $a starts at the first input.. obviously. The first input should be less or equal to the second input. i.e. 192.x.x.x 192..193.x.x.x for ([int]$a = $ip1[0];($a -le $ip2[0]); $a++) { # Say we're interating through a ton of IPs.. eventually when we get to where we want to be.. we need to reset the limit to stop at our second input # input will never change.. but ip1 and ip2 can because they are our limiters. if ($a -eq $ip2[0]) { [array]$ip2 = $input2 -split ".",0,"simplematch" } # This is the first break.. don't worry about how it's spelled. In order for the loop to break properly.. we had to add a mechanism to exit each nested for. # I.E. when b.c.d. reaches 255.. that was the easiest way to go back to the previous for loop in order to interate to the next subnet. [int]$brakeB = 0 # $b starts at the first inputs second octet. Checks to see if it's less or equal to the second input's second octet OR if $a is less than the input2's first octet. # I.E. 65.77.120.1 , 66.20.50.1. Those are two valid ranges.. but input1 $b > input2 $b. but... 65 < 66.. so we're still good. for ([int]$b = $ip1[1];($b -le $ip2[1] -or $a -lt $ip2[0]) -and [int]$brakeB -eq 0; $b++) { # So.. We have to tell the sweep where to stop when it's iterating.. or.. it'll just keep going. a.b.c.d is our count. ip1 and ip2 are constants/limiters # We want $c to be 255 when we want to add to $b if ($b -lt $ip2[1] -or $a -lt $ip2[0]) { [int]$ip2[2] = 255 } # This checks if $b gets to 255 or 256 and needs to keep going. # First it checks if a is less than ip2's first octet, if it is.. it sets the limit of b to 256, because if it equals 255 it will auto reset to 0 if you start on the 255 subnet if ($a -lt $ip2[0]) { $ip2[1] = 256 # so if $b is 256... that means it wants to keep going.. so reset and break the loop. if ($b -eq $ip2[1]) { $b = 0 $ip1[1] = 0 [int]$brakeB++ } } # This is here to be able to break out of the $c loop $brakeC = 0 # $c starts at input1's third octet. Checks against ip2's a.b.c. If $c less or equal to ip2 OR b < inpute's second octet, or a < input2's first octet. # The breaks make sure that it's supposed to run at that current time for ([int]$c = $ip1[2];($c -le $ip2[2] -or $b -lt $ip2[1] -or $a -lt $ip2[0]) -and $brakeB -eq 0 -and $brakeC -eq 0; $c++) { # if c is less than input2's third octet, it sets the limit to 255 if ($c -lt $ip2[2]) { [int]$ip2[3] = 255 } # if c is equal to input2's third octet, reset the constant so it'll stop if our input told it to elseif ($c -eq $ip2[2]) { [array]$ip2 = $input2 -split ".",0,"simplematch" } # This checks to see if b < input2's second octet or if a < input2's first octet elseif ($b -lt $ip2[1] -or $a -lt $ip2[0]) { # so if $c is 256... that means it wants to keep going.. so reset and break the loop. if ($c -eq 256) { $c = 0 $ip1[2] = 0 $brakeC++ } } # This is here to be able to break out of the $d loop $brakeD = 0 # d starts at input1's fourth octet. checks to see if it's less or equal to it's input2 counterpart # checks to see if a.b.c are less than it's counterpart and if it's supposed to be running for ([int]$d = $ip1[3];($d -le $ip2[3] -or $c -lt $ip2[2] -or $b -lt $ip2[1] -or $a -lt $ip2[0]) -and $BrakeC -eq 0 -and $brakeD -eq 0; $d++) { ping -n 1 "$a.$b.$c.$d" [array]$ip2 = $input2 -split ".",0,"simplematch" if ($c -lt $ip2[2] -or $b -lt $ip2[1] -or $a -lt $ip2[0]) { if ($d -eq 255) { $d = 1 $ip1[3] = 1 $brakeD++ } } } } } }