Posted On January 30, 2023

PowerShell: Quickly Test Connectivity from a List of Sources toward a Destination on a Specific Port

kimconnect 0 comments
blog.KimConnect.com >> Codes , Networking , Windows >> PowerShell: Quickly Test Connectivity from a List of Sources toward a Destination on a Specific Port
# testRemotePort.ps1

$connectFrom=@'
windows1
windows2
'@
$connectTo=@'
\\servername\sharename
'@
$testPort=445

$sources=@($connectFrom -split "`n")|%{$_.Trim()}
$destinations=@($connectTo -split "`n")|%{$_.Trim()}
$results=@()
foreach($source in $sources){
    foreach($destination in $destinations){
        $result=invoke-command -computername $source {
            param($destination,$testPort)
            write-host "$env:computername to $destination`:$testPort => " -nonewline
            $result=try{if(test-netconnection $destination -port $testPort -informationLevel Quiet){'Open'}else{'Closed'}}catch{write-warning $_;'Unknown'}
            write-host "$result"
            return $result
        } -Args $destination,$testPort
        $results+=[pscustomobject]@{
            source=$source
            destination=$destination
            port=$testPort
            status=$result
        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post

PowerShell: Get Windows Resource Utilization

# getWindowsResourceUtilization.ps1 # version 0.02 # Gather information on a list of Windows Machines #…

Basic HTML and HTML5: Create an Ordered List

<h2>CatPhotoApp</h2><main><p>Click here to view more <a href="#">cat photos</a>.</p><a href="#"><img src="https://bit.ly/fcc-relaxing-cat" alt="A cute orange cat lying…

PowerShell: Set DNS Records on Remote Computers

# setDnsEntries.ps1 $computernames=@( "$env:computername" ) $dnsServers=@( "8.8.8.8", "4.4.2.2" ) $results=[hashtable]@{} foreach ($computername in $computernames){ $psSession=new-pssession…