Advertisement
cwprogram

[Revised] Powershell TCP Port Connection Test

Jul 11th, 2011
725
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#
  2. Description:
  3. A function to test if a connection can be made to a specific TCP port
  4.  
  5. Parameters:
  6.  
  7. $hostname [String] - The hostname to connect to, ie. www.google.com
  8. $port [Int32] - The port to connect to
  9.  
  10. Return Value:
  11. This function returns a custom PSObject of the following format:
  12.  
  13. $object.Status = True/False (Whether or not the test passed)
  14. $object.Exception = The exception that occured if the test failed
  15. #>
  16. function Test-TcpPortConnection {
  17.     param($hostname,$port)
  18.     try {
  19.         $tcp = New-Object System.Net.Sockets.TcpClient
  20.         $tcp.Connect($hostname,$port)
  21.         $tcp.Close()
  22.         return New-Object PSObject -Property @{
  23.             Pass=$true;
  24.             Exception=$null;
  25.         }
  26.     }
  27.     catch [System.ArgumentNullException] {
  28.         return New-Object PSObject -Property @{
  29.             Pass=$false;
  30.             Exception="Null argument passed";
  31.         }
  32.     }
  33.     catch [ArgumentOutOfRangeException] {
  34.         return New-Object PSObject -Property @{
  35.             Pass=$false;
  36.             Exception="The port is not between MinPort and MaxPort";
  37.         }
  38.     }
  39.     catch [System.Net.Sockets.SocketException] {
  40.         return New-Object PSObject -Property @{
  41.             Pass=$false;
  42.             Exception="Socket exception";
  43.         }        
  44.     }
  45.     catch [System.ObjectDisposedException] {
  46.         return New-Object PSObject -Property @{
  47.             Pass=$false;
  48.             Exception="TcpClient is closed";
  49.         }
  50.     }
  51.     catch {
  52.         return New-Object PSObject -Property @{
  53.             Pass=$false;
  54.             Exception="Unhandled Error";
  55.         }    
  56.     }
  57. }
  58.  
  59. # Returns True unless your network is messed up
  60. $result = Test-TcpPortConnection "www.google.com" 80
  61. if($result.Pass) {
  62.     Write-Host "Test passed: www.google.com:80"
  63. }
  64. # Will fail unless your dns resolves this
  65. $result = Test-TcpPortConnection "www.thisisnotarealhost.com" 80
  66. if(!$result.Pass) {
  67.     Write-Host "An exception occured:" $result.Exception
  68. }
  69.  
  70. # Will fail because of invalid port range
  71. $result = Test-TcpPortConnection "www.google.com" 100000000
  72. if(!$result.Pass) {
  73.     Write-Host "An exception occured:" $result.Exception
  74. }
  75.  
  76. <# Sample Output:
  77. Test passed: www.google.com:80
  78. An exception occured: Socket exception
  79. An exception occured: The port is not between MinPort and MaxPort
  80. #>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement