‚Č° Menu

PowerShell: Test port connectivity

We being system administrators come across requirements to see if a port is open for connections or not. For example, to see if a web service is working, the first thing I do is, “telnet webservername 80”. Similarly every client/server application some or other port for establishing connection and communication. Since the port connectivity verification is pretty much part of system administrator’s life, I want to do it with my favorite programming language, “PowerShell”.

Below is the function that tests the port connection and returns the status.

Code:

function Test-PortConnection {            
[cmdletbinding()]            
param(                        

[parameter(mandatory=$true)]            
[string]$TargetHost,                        

[parameter(mandatory=$true)]            
[int32]$TargetPort,                        

[int32] $Timeout = 10000                        

)                        

$outputobj = New-Object -TypeName PSobject            

$outputobj | Add-Member -MemberType NoteProperty -Name TargetHostName -Value $TargetHost            

if(test-Connection -ComputerName $TargetHost -count 2) {            
    $outputobj | Add-Member -MemberType NoteProperty -Name TargetHostStatus -Value "ONLINE"            
} else {            
    $outputobj | Add-Member -MemberType NoteProperty -Name TargetHostStatus -Value "OFFLINE"            
}            

$outputobj | Add-Member -MemberType NoteProperty -Name PortNumber -Value $targetport            

$Socket = New-Object System.Net.Sockets.TCPClient            
$Connection = $Socket.BeginConnect($Targethost,$TargetPort,$null,$null)            
$Connection.AsyncWaitHandle.WaitOne($timeout,$false)  | Out-Null            

if($Socket.Connected -eq $true) {            
    $outputobj | Add-Member -MemberType NoteProperty -Name ConnectionStatus -Value "Success"            
} else {            
    $outputobj | Add-Member -MemberType NoteProperty -Name ConnectionStatus -Value "Failed"            
}            

$Socket.Close | Out-Null                        
$outputobj | select TargetHostName, TargetHostStatus, PortNumber, Connectionstatus | ft -AutoSize            

}

In this code, I am using “System.Net.Socket.TCPClient” dotnet object to verify the connection. One the script tries to establish the connection it waits for at least 10 seconds to see if the other party is responding or not on the port I am querying. It is because, based on the server responsiveness and other factors, you may get some delayed response. To facilitate such incidents, I am using 10 seconds default delay. However, if you wish you can change it to the value you want by simply passing the value through -TimeOut parameter. The script takes other two mandatory parameters. One is -TargetHost¬† and another is -TargetPort. The “TargetHost” parameter accepts the remote server name on which you want to test the port connectivity and Targetport is the actual port number you want to query.

Usage example:

Hope this information helps…

 

{ 2 comments… add one }

Leave a Comment