i’m sruggeling with some script that checks NW connectivity between hosts basicaly i have CSV with source IP and port which looks like this
the problem is when i run this script it checks me every ip with every port and i need it to check onlu port that is next to it i .e for 8.8.8.8 only port 53 and for 1.1.1.1 only 443
below is my code
$dest = Import-Csv "C:tempdest.csv"
#$dest | ft
$source = (Get-WmiObject Win32_Computersystem).name
Foreach ($d in $dest.SourceIP){
Foreach ($P in $dest.Port){
try{
$temp = new-object Net.Sockets.TcpClient
$temp.Connect("$d", $P)
if ($temp.Connected -eq $true){
Write-Host $P -ForegroundColor Green " => port is open from" $source "to" $d
}
}
catch {$theError =$_
if($theError.Exception -like "*A connection attempt failed*"){
Write-Host $P -ForegroundColor Red "=> NO TRAFFIC between" $source "and" $d
}
elseif($theError.Exception -like "*No connection could be made*"){
Write-Host $P -ForegroundColor Red "=> port is closed from" $source "to" $d
}
}
}
}
I know that nested loop is the problem here but i ran oout of ideas how to change it to make it work properly