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

HP Procurve Configure Time and Default Gateway

#configure gateway and NTPip default-gateway 10.10.10.1timesync sntpsntp unicastsntp 30# These are ntp.org servers for North…

Another Logon Script Example from 2008

Method 1 - Mixed Environment:1. Edit the following script as appropriate2. Then, save such script…

PowerShell: Add and Remove a Registry Key

How to Add a Registry Key # Add New Registry Key $regHive='REGISTRY::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSCRM' $keyname='OleDbTimeout' $value=600 Set-ItemProperty…