Showing posts with label Custom. Show all posts
Showing posts with label Custom. Show all posts

Thursday, July 25, 2024

Test-NetConnection using custom source port.

Powershell utility to test remote port using a custom source address from your computer.

 

If you have multiple ip addresses assigned on your computer the Test-Netconnection utility does not allow you to test the connection to a remote computer using  one of those addresses. 


The script below addresses that drawback.

 #Usage: .\Test-Connect.ps1 -computername 10.10.10.10 -RemotePort 389 -sourceipAddress 123.23.10.5

param([string]$computername,[string]$RemotePort,[string]$sourceipAddress)
$destHostName = $computername
$destPort     = $RemotePort
$src  = [System.Net.IPEndPoint]::new([ipaddress]::Parse($sourceipAddress),0)
$tc   = [System.Net.Sockets.TcpClient]::new($src)
$tc.Connect($destHostName,$destPort)

if ($tc.Connected) {
    "Connected!"
} else {
    "Not connected"
}

$tc.Dispose()