Skip to content

Instantly share code, notes, and snippets.

@mbrownnycnyc
Created November 16, 2018 19:55
Show Gist options
  • Save mbrownnycnyc/68cf756096b05da1a684d8cd1fb4c368 to your computer and use it in GitHub Desktop.
Save mbrownnycnyc/68cf756096b05da1a684d8cd1fb4c368 to your computer and use it in GitHub Desktop.
test connectivity of a tcp port
function test-tcpport {
param (
[int]$port = 80,
[string]$ip = "127.0.0.1"
)
try {
$socket = new-object System.Net.Sockets.TcpClient
$timespan = [TimeSpan]::FromMilliseconds(500)
if ( ($socket.connectasync($ip, $port)).wait($timespan) ) {
return $true
} else {
return $false
}
} catch {
return $false
} finally {
$socket.close() | out-null
$socket = $null
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment