简体   繁体   中英

Integer incrementing 2 times in foreach loop

I'm trying to figure out why my "$count--" is incrementing the integer down by 2 every time it cycles to a new IP. I want this to count down by 1 every time a new IP is being checked.

function scrape {
$PortList = @(443, 4433, 444, 433, 4343, 4444, 4443)
$IPList = $text1.text.split("`r")
$IPList = $text1.text.split()
$count = ($IPList.Count - 1)/2
Write-Host $IPList
Write-Host 'Firewalls with open ports will be listed here as they are discovered. Please wait while the script' `n 'processes the list of IP addresses. There are'$count 'IP addresses to check'

foreach ($IP in $IPList) {
   $count--
   foreach ($Port in $PortList) {
      $url = "https://${IP}:${Port}"
      $verb = 'GET'
      $SiteData = try{httpget $url $verb}Catch{Continue}
      If ($SiteData.Contains("auth1.html")) {
         Write-Host ('https://' + $IP + ':' + $Port + "   MGMT  " + $IP) -ForegroundColor Red
         $text2.text += ('https://' + $IP + ':' + $Port + "   MGMT  " + $IP + "`n")
      }
      Else {
         If ($SiteData.Contains("SSLVPN")) {
            Write-Host ('https://' + $IP + ':' + $Port + "   SSLVPN  " + $IP)
            $text2.text += ('https://' + $IP + ':' + $Port + "   SSLVPN  " + $IP + "`n")
         }
         Else {
            Write-Host ('https://' + $IP + ':' + $Port + "   OTHER  " + $IP)
            $text2.text += ('https://' + $IP + ':' + $Port + "   OTHER  " + $IP + "`n")
         }
      } 
   }
}
}

EDIT/UPDATE: Okay so I figured out that the loop is counting blank space between the IP addresesses as a member of the array, which is causing that double decrement. Now I just have to figure out how to have it only count the addresses.

This looks suspicious:

$count = (15 - 1)/2

Have you tried running your script and outputting the value of $count from within the foreach loop to confirm that the value is what you think it is?

I also think you need to move the incremented statement AFTER doing the work.

Using the example below, the count output will return the following:

  • count: + 3 count: + 2 count: + 1 count: + 0
    function scrape {
    $PortList = @(443, 4433, 444, 433, 4343, 4444, 4443)
    #$IPList = $text1.text.split("`r")
    #$IPList = $text1.text.split()
    $IPList = ("IP:A", "IP:B", "IP:C", "IP:D")
    $count = ($IPList.Count - 1)
    Write-Host $IPList
    #Write-Host 'Firewalls with open ports will be listed here as they are     discovered. Please wait while the script' `n 'processes the list of IP addresses. There are'$count 'IP addresses to check'

    foreach ($IP in $IPList) {
       Write-Host "count: " + $count

       # go do some stuff
       $count--

    }
    }

As you've since noticed, your problem is that your $IPList array contains empty elements between the IP addresses to examine.

The reason is that neither of your attempts to split a multi-line string into individual lines works correctly:

$text1.text.split("`r") # !! retains the `n after `r -> elements start with `n
$text1.text.split()     # !! splits by *both* `r and `n -> empty extra elements

The simplest solution is to use the unary form of the -split operator , which conveniently splits a string into tokens by any runs of whitespace , including newlines (irrespective of whether they are Windows-style CRLF newlines ( `r`n ) or Unix-style LF-only newlines ( `n )):

-split $text1.text # split by runs of whitespace, including newlines

Note, however, that if the individual lines contain intra-line whitespace (spaces, tabs), they will be broken into tokens as well.

If you truly only want to split by newlines (line breaks), use the binary form of the -split operator :

$text1.text -split '\r?\n' # split by CRLF or LF

Note that -split 's RHS is a regular expression ; \\r?\\n is a pattern that matches both CRLF and LF-only newlines.

If you know that the newlines are only ever the platform-appropriate ones, you can use
$text1.text -split [Environment]::NewLine .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM