How to Monitor specific TCP-Port on using PowerShell and Test-NetConnection

Are you in need of a simple and efficient solution to monitor a specific TCP port? Look no further! In this blog post, we’ll walk you through how to use PowerShell and the Test-NetConnection cmdlet to check if a specific TCP port is open every 3 seconds. This method will help you keep an eye on your network connections and troubleshoot any potential issues.

What is PowerShell and Test-NetConnection?

PowerShell is a powerful scripting language and shell developed by Microsoft. It allows administrators to automate tasks and manage various aspects of Windows and Linux systems.

Test-NetConnection is a versatile cmdlet in PowerShell that tests network connectivity to a remote host, including the ability to test specific ports. It is a useful tool for network troubleshooting and identifying connection issues.

Monitoring Port 443 on blog.solvia.ch

Follow the steps below to create and run a PowerShell script that checks if port 8443 on gate.host12.solvia.ch is open every 3 seconds:

  1. Open PowerShell.
  2. Copy and paste the following code block into the PowerShell console:
while ($true) {
    $targetHost = "blog.solvia.ch"
    $targetPort = 443
    $result = Test-NetConnection -ComputerName $targetHost -Port $targetPort
    if ($result.TcpTestSucceeded) {
        Write-Host "$(Get-Date): Port $targetPort on $targetHost is open" -ForegroundColor Green
    } else {
        Write-Host "$(Get-Date): Port $targetPort on $targetHost is closed" -ForegroundColor Red
    }
    Start-Sleep -Seconds 3
}
  1. Press Enter to run the script. The script will continuously check if port 443 on blog.solvia.ch is open and display the result along with a timestamp.
  2. To stop the script, press Ctrl+C.

Now you have a simple and efficient way to monitor port 443 on blog.solvia.ch using PowerShell and Test-NetConnection. This script is particularly useful for network administrators who want to keep an eye on their network connections and troubleshoot any potential issues in real-time. Happy monitoring!

Testing the Script with a Fake Hosts Entry

In order to test the script effectively, it’s essential to create a scenario where the script can detect a closed port. One way to do this is by adding a fake hosts entry pointing to an IP address where port 443 is not open or listening. This section will guide you through the process.

  • Open the hosts file on your computer with administrator privileges. The location of the file is typically:
    • On Windows: C:\Windows\System32\drivers\etc\hosts
    • On macOS and Linux: /etc/hosts
  • Add a new line at the end of the file with the following format:
[IP_ADDRESS] blog.solvia.ch
  • Replace [IP_ADDRESS] with an IP address where port 443 is not open/listening. For example, you could use 127.0.0.2, assuming you don’t have a web server running on this local IP with port 443 open.
  • Save the changes to the hosts file and close the text editor.
  • Next, run the PowerShell script provided in the “Monitoring Port 443 on blog.solvia.ch” section. The script should now report that port 443 on blog.solvia.ch is closed.
  • Finally, to revert the changes and resume normal operation, remove the line you added to the hosts file in step 2, save the changes, and close the text editor.

with this fake host entry you will get:

By following this testing guide, you can ensure that the PowerShell script is working correctly and can detect closed ports. In conclusion, this script will help you monitor port 443 on blog.solvia.ch and troubleshoot any potential issues effectively. Happy monitoring and testing!