Tech Support Corner: Using PowerShell to Troubleshoot TCP Ports

4 min read

May 5, 2022 2:00:00 PM


Do you find yourself in need of troubleshooting a TCP connection, but your IT department has everything locked down, so you're unable to install third-party Troubleshooting tools without permission? An often overlooked but effective tool for troubleshooting TCP socket connections is PowerShell.

Continuing our Tech Support Corner blog series, this blog post covers how to use PowerShell to effectively determine what issues you may be having with a problematic TCP Ethernet connection.

In a situation where your IT department doesn't permit installation of tools like Wireshark or other third-party diagnostics utilities, being able to utilize built-in Windows tools is a must. So you are in luck because, today, we will learn how to utilize PowerShell, which is a tool that your operating system already has available that will allow you to test TCP connections.

Before moving forward, we strongly suggest you take a few minutes to read our Tech Support Corner: "It has to work, I can ping it, right?" post. This previous post will provide you with a great foundation for why PING commands are not ideal for testing TCP Connection, as well as, some other tools that can be useful. Of course, if you are already familiar with how ICMP (Internet Control Message Protocol) echo requests (i.e. pings) work, feel free to continue with the rest of this post.

What is the Test-NetConnection Cmdlet?

First of all, PowerShell is a powerful modern command shell with a scripting language and configuration management framework that allows power users to perform many advanced operations across multiple platforms including Windows, Linux and macOS. The Test-NetConnection cmdlet is a part of the NetTCPIP module included with PowerShell v4.0, and it allows users to check network connections with just a few words. This helpful command offers a comprehensive alternative to some network admin tools such as ping, traceroute, and TCP Port scanner and you will see why.

Let's say that you are in the process of setting up a connection with a Modbus device with the IP address 192.168.111.121, and you are using our TOP Server Modbus Suite OPC server to connect to it on TCP port 502 but you are getting the following errors:

Screenshot_TOPServer_EventLog_SocketError

Naturally, your first instinct might be to execute a ping command. However, when the command succeeds, as shown in the screenshot below, and you're left then thinking that something must be wrong with the OPC server, because clearly the device is responding to pings from the same computer. Here is where we need to remember that ICMP requests do not tell us a whole lot about the state of the port we are trying to connect to.

Screenshot_SuccessfulPing

And here is how that same issue displays in PowerShell using the Test-NetConnection Cmdlet (Please keep in mind that, in the command below, we used port 501 instead of 502 to purposely show that ping commands can be misleading when dealing with TCP connections since they don't take into account the specific TCP port):

Screenshot_PowerShell_Test_NetConnnetion_Example

NOTE: While PingSucceeded was True in this example, it is important to note that a response might return a False for the PingSucceeded. In such cases, it is likely that ICMP requests have been disabled on the remote server/device.

Steps to Follow When You Have a Failed TCP Socket Connection

We recommend trying the following steps the next time you have a problematic TCP socket connection your are attempting to troubleshoot:

  1. Open PowerShell (enter "PowerShell" in the Windows search) and type the following command and hit enter: tnc <TargetMachine> -port <PortNumber>

    Screenshot_PowerShell_Example_Command_Test_NetConnection
  2. Wait for the command to return a response. Now, if the device was having issues where it powered off or it got disconnected from the network, you should see a response like this one:

    Screenshot_PowerShell_Example_Command_Test_NetConnection_BothFalse
  3. Alternately, what you should see if the connection is healthy (i.e. TOP Server should be able to successfully connect), then you would see the following response in PowerShell:

    Screenshot_PowerShell_Example_Command_Test_NetConnection_Successful
    This response tells us specifically that the Port 502 is open and Test-NetConnection module was able to validate TCP handshake, so the port should be ready to establish a socket connection.

So, in conclusion, PowerShell is another tool for your troubleshooting arsenal to help you quickly resolve issues interfering with successful connection. Again, if you haven't read our other post on other alternatives to Ping for network troubleshooting, we encourage you to get caught up here.

Don't forget to subscribe to our blog to get more useful tech support tips like this one and for the latest Software Toolbox product news every week.

Click to Subscribe to SWTB Blog

Rodrigo Restrepo
Written by Rodrigo Restrepo

Software Toolbox Technical Blog

We're engineers like you, so this blog focuses on "How to" appnotes, videos, tech team tips, product update announcements, user case studies, and other technical updates.  Subscribe to updates below. Your feedback and questions on posts are always welcomed - just use the area at the bottom of any post.

Subscribe to our Blog

Recent Posts

Posts by Topic

See all