Test Connectivity of TCP Ports on remote machine(s)
Starting from PowerShell 4.0 We have got a pretty neat
cmdlet that is Test-NetConnection
which works for testing TCP Ports as well as does a ping test on remote endpoints
(servers).
Ex: PS C:\>
Test-NetConnection localhost -Port 3389
The only drawback with Test-NetConnection
is that it does not have a parameter for specifying TimeOut value to return the result or fail if a response is not
received within 'x' milliseconds which is very time consuming and sometimes frustrating
while doing port connectivity test for bulk hosts.
Here is a Quick PowerShell
Function which can be used to test TCP ports on Remote Endpoints with complete
control at your fingertips which lets you specify whatever TimeOut period you want it to return after getting a response or connection
refusal from the target.
function Test-Port
{
Param($HostName, $TcpPort, $MsTimeOut)
$tcpClient = New-Object System.Net.Sockets.TcpClient
$connection = $tcpClient.BeginConnect($HostName,$TcpPort,$null,$null)
$asyncResult = $connection.AsyncWaitHandle.WaitOne($MsTimeOut,$false)
"$HostName : $TcpPort : $asyncResult"
}
Now that we have the function ready but we wont be able to use
it simply as a PowerShell Script as its written as a function which would need
to be loaded into memory before being able to execute it.
The best thing with PowerShell Functions is, it is used as a
simple cmdlet instead of a being used as a PowerShell Script and you don’t really
need to bother where it located to be able to execute it. It just has one
caveat which is loading it in memory which is also called Dot Sourcing.
How do we do Dot sourcing?
- Just copy the code and save it as a PS1 Script (Test-Port.ps1)
- Execute it prefixing with an extra dot “.” (Eg. PS C:\>. .\Test-Port.ps1 )
- Done!!
Now name of the function should be available as a PowerShell
cmdlet in that specific PowerShell Window.
Remember that this cmdlet will only
be available in that specific PowerShell console as the Function has only been
loaded in that specific PowerShell Console. But wait.. there is workaround too, We can get all our
functions loaded in every PowerShell console we launch.
How to do that :
·
Open a Powershell Console.
·
Type $PROFILE and hit enter, It will display a PS1
File under your home directory:
PS C:\> $PROFILE
C:\Users\prakakum\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
·
Open this ps1 PS Profile script in notepad/ISE/any
text editor and add whatever functions you have to it. For example we will add
this Test-Port Function here.
Save and close the File, Now every PowerShell
Console we open should have this function available as a native cmdlet.
Here is how we can use this
function (any function) to run on multiple servers from a text file.
Thank You :)
Post your comments/suggestions below
Here is the enhanced version of this script which returns result as objects in which they can be filtered/selected as needed:
ReplyDeletefunction Test-Port {
Param($ComputerName,$TcpPort,$msTimeOut)
$tcpClient = New-Object System.Net.Sockets.TcpClient
$connection = $tcpClient.BeginConnect($ComputerName,$TcpPort,$null,$null)
$asyncResult = $connection.AsyncWaitHandle.WaitOne($MsTimeOut,$false)
$Output = @{
ComputerName = $ComputerName
TCPPort = $TcpPort
Result = $asyncResult
}
New-Object PSObject -Property $Output |select ComputerName, TcpPort, Result
}
Hello Prakash, everyone,
ReplyDeleteunfortunately the script does not work as desired.
Unopened ports are output as true.
example (Port 8443 doesn't open):
test port 192.168.4.100 8443 10000
ComputerName TCPPort Result
------------ ------- ------
192,168,4,100 8443 True