Using Test-NetConnection to check connectivity
A regular ping
command is great for checking if Host A can see Host B, but what if we want to go a bit deeper- for example check if a web server is contactable on port 80 and 443? The PowerShell cmdlet Test-NetConnection
is a great tool for doing this.
It’s become a key part of my troubleshooting toolkit- is the server contactable? Is there a firewall blocking the traffic? Is the web server service running? Test-NetConnection gives us a simple yes/no and some other useful information too.
Examples:
A working connection to port 80 (HTTP) on the server
1PS C:\> Test-NetConnection -ComputerName Server1.MyDomain.Com -Port 80
2
3ComputerName : Server1.MyDomain.Com
4RemoteAddress : 10.0.0.34
5RemotePort : 80
6InterfaceAlias : Local Area Connection
7SourceAddress : 10.0.0.17
8PingSucceeded : True
9PingReplyDetails (RTT) : 14 ms
10TcpTestSucceeded : True
A failed connection to port 443 (HTTPS) on the same server
1PS C:\> Test-NetConnection -ComputerName Server1.MyDomain.Com -Port 443
2WARNING: TCP connect to Server1.MyDomain.Com:443 failed
3
4ComputerName : Server1.MyDomain.Com
5RemoteAddress : 10.0.0.34
6RemotePort : 443
7InterfaceAlias : Local Area Connection
8SourceAddress : 10.0.0.17
9PingSucceeded : True
10PingReplyDetails (RTT) : 22 ms
11TcpTestSucceeded : False
From this we can quickly and easily establish that whilst communication with the server is possible on port 80, traffic on port 443 is not being received- the first things I would check here would be that the webserver service is running correctly, configured to accept HTTPS traffic, and not being blocked the local firewall on the server.