Created
November 16, 2018 19:55
-
-
Save mbrownnycnyc/68cf756096b05da1a684d8cd1fb4c368 to your computer and use it in GitHub Desktop.
test connectivity of a tcp port
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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