Advertisement
cwprogram

Powershell TCP Port Test Function

Jul 11th, 2011
439
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function Test-TcpPortConnection {
  2.     param($hostname,$port)
  3.     try {
  4.         $tcp = New-Object System.Net.Sockets.TcpClient($hostname,$port)
  5.         $tcp.Close()
  6.         return $true;
  7.     }
  8.     catch {
  9.         return $false;
  10.     }
  11. }
  12.  
  13. // Returns True unless your network is messed up
  14. Test-TcpPortConnection "www.google.com" 80
  15. // Returns False unless your dns resolves this
  16. Test-TcpPortConnection "www.thisisnotarealhost.com" 80
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement