Powershell function with no errors and no results

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

Upvotes: 1

Views: 92

Answers (2)

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!

Upvotes: 1

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174990

$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"
                }   
            }
        }
    }
}

Upvotes: 3

Related Questions