bashbeta

Untitled

Nov 5th, 2022
1,598
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function testport
  2.  {
  3.     param
  4.     (
  5.        
  6.         [Parameter(Mandatory = $True)]
  7.         [string]$hostname,
  8.         [string]$port,
  9.         [string]$timeout
  10.  
  11.     )
  12.  
  13.     $requestcallback = $null
  14.     $state = $null
  15.  
  16.     try {
  17.             $client = New-Object System.Net.Sockets.TcpClient
  18.  
  19.             $client.BeginConnect($hostname,$port,$requestCallback,$state)
  20.    
  21.             Start-Sleep -milli $timeOut
  22.  
  23.     if ($client.Connected)
  24.     {
  25.          $open = $true
  26.     }
  27.    
  28.     else
  29.     {
  30.         $open = $false
  31.     }
  32.  
  33.  
  34.        }
  35.  
  36.     catch {
  37.  
  38.         write-host "Error connecting to socket!"
  39.     }
  40.    
  41.  
  42.     $client.Close()
  43.  
  44.     return $open
  45.  
  46. }
Advertisement
Add Comment
Please, Sign In to add comment