1

I would like to scan the batch of my local networks to find all standalone Wi-Fi AP. I'm going to archive this by checking the TCP/80 port. I have 89 networks in the text file. My script is below. The script seems to run but I get no output either to the screen or to any file. I would be very appreciative if someone could tell me what's wrong.

#PSVersion 7.1.3 #-----------------------------------------

function Test-Port() {
    [CmdletBinding()]
    param (
        [Parameter(ValueFromPipeline = $true, Mandatory = $true)]
        [Alias('tp')]
        [string[]]$network,
        [int]$port = 80
    )
    #Begin { Write-Verbose "Scanning network $network"}
    Process {
        1..254 | ForEach-Object -Parallel -ThrottleLimit 10 {
            $ip = "$network.$_"
            If ($(Test-netConnection –ComputerName $ip -Port $port -ErrorAction SilentlyContinue).TcpTestSucceeded ) {
                Write-Output "$ip port $port open" | Out-File "C:\tmp\$network.txt"
            }   
        }
    }
}

[string[]]$lans = Get-Content -Path "C:\tmp\lans.txt"
   
#$lans | Test-Port
Test-Port -network $lans

====== Even this doesn't work with -Parallel. It just doesn't see the $n.

[string[]]$networks = Get-Content -Path "C:\tmp\lans.txt"
[string[]]$a = 1..50
foreach ($n in $networks) {  
     ForEach-Object -InputObject ($a) -parallel {
         write-output $n
     }  }

And it works fine without -parallel

7
  • Do you know the function works? Is the variable $lans populated? Commented Jul 23, 2021 at 16:08
  • so ... if you strip out all the function stuff and just run the core code ... where does it stop outputting info? Commented Jul 23, 2021 at 16:09
  • @Alex_P Yes, $lans populated. i.e. ... 192.168.3 10.2.18 10.2.30 10.2.11 10.2.18 10.222.176 ...etc Commented Jul 23, 2021 at 18:04
  • @Lee_Dailey it works fine out of function. And moreover, it works fine inside the function if I remove -Parallel -ThrottleLimit 10 Commented Jul 23, 2021 at 18:28
  • Outside the function. And without -Parallel. Just this part: ----- [string[]]$networks = Get-Content -Path "C:\tmp\lans.txt" foreach ($net in $networks) { 1..254 | ForEach-Object { $ip = "$net.$_" If ($(Test-netConnection –ComputerName $ip -Port 80 -ErrorAction SilentlyContinue).TcpTestSucceeded ) { Write-Output "$ip port $port open" | Out-File "C:\tmp\$net.txt" -Append } } } ----- Works fine. But it's very slow because of the quantity of IP addresses. If I add -Parallel I see WARNING: Name resolution of .88 failed Commented Jul 23, 2021 at 18:45

2 Answers 2

3

$network is a string array, you need to loop over it :)

function Test-Port() {
    [CmdletBinding()]
    param (
        [Parameter(ValueFromPipeline = $true, Mandatory = $true)]
        [Alias('tp')]
        [string[]]$network,
        [int]$port = 80
    )
    #Begin { Write-Verbose "Scanning network $network"}
    Process {
        foreach($net in $network){
            1..254 | ForEach-Object -Parallel -ThrottleLimit 10 {
                $ip = "$net.$_"
                If ($(Test-netConnection –ComputerName $ip -Port $port -ErrorAction SilentlyContinue).TcpTestSucceeded ) {
                    Write-Output "$ip port $port open" | Out-File "C:\tmp\$network.txt"
                }   
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Of course :). You're right. I fixed this part. And change Out-File to Out-File "C:\tmp\$net.txt" -Append. BUT. It doesn't work anyway. Maybe I'm blind, but I don't see the mistake....
So, it works fine after I removed -Parallel -ThrottleLimit 10 part. What a pity.
1

Finally! The trick is $using

$ip = "$($using:net).$_"

function Test-Port() {
    [CmdletBinding()]
    param (
        [Parameter(ValueFromPipeline = $true, Mandatory = $true)]
        [Alias('tp')]
        [string[]]$networks
    )
    Process {
        foreach ($net in $networks) {  
            1..254 | ForEach-Object -Parallel {
                $ip = "$($using:net).$_"
                If ($((Test-NetConnection –ComputerName $ip -Port 80 -InformationLevel Quiet).TcpTestSucceeded)) {
                Write-Output "$ip port 80 is open"  | Out-File "C:\tmp\$($using:net).txt" -Append
                } 
            }  
        }
    }
}


[string[]]$networks = Get-Content -Path "C:\tmp\lans.txt"

Test-Port -networks $networks

It works. Thanks to all for your help, guys!

1 Comment

Please mark Mathias post as the answer if he answered your request.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.