PoshCode Logo PowerShell Code Repository

Difference between
modified post 6799 by teste on Thu 16th Mar 12:30 and
original post 85 by BSonPosh on Mon 31st Dec 14:18
Showold version | new version | both versions

    
11
Param([string]$srv,$port=135,$timeout=3000,[switch]$verbose)
33
# Test-Port.ps1
44
# Does a TCP connection on specified port (135 by default)
66
$ErrorActionPreference = "SilentlyContinue"
88
# Create TCP Client
99
$tcpclient = new-Object system.Net.Sockets.TcpClient
1111
# Tell TCP Client to connect to machine on Port
1212
$iar = $tcpclient.BeginConnect($srv,$port,$null,$null)
1414
# Set the wait time
1515
$wait = $iar.AsyncWaitHandle.WaitOne($timeout,$false)
1717
# Check to see if the connection is done
1818
if(!$wait)
1919
{
2020
    # Close the connection and report timeout
2121
    $tcpclient.Close()
2222
    if($verbose){Write-Host "Connection Timeout"}
2323
    Return $false
2424
}
2525
else
2626
{
2727
    # Close the connection and report the error if there is one
2828
    $error.Clear()
2929
    $tcpclient.EndConnect($iar) | out-Null
3030
    if(!$?){if($verbose){write-host $error[0]};$failed = $true}
3131
    $tcpclient.Close()
3232
}
3434
# Return $true if connection Establish else $False
3535
if($failed){return $false}else{return $true}

ContributeMost Recent Contributions (feed)

Contribute ... Next Page