- Create a certificate-signed RDP shortcut via Group Policy - Fri, Aug 9 2019
- Monitor web server uptime with a PowerShell script - Tue, Aug 6 2019
- How to build a PowerShell inventory script for Windows Servers - Fri, Aug 2 2019
Before we get into the fancy PowerShell commands though, do you remember good ol' ping.exe? That utility is in every sysadmin's toolbelt. The ping utility tests a server's network connection using ICMP. Responding to ICMP doesn't always mean a server is "up," but it's better than nothing!
For example, I have an Azure virtual machine online but with its network security locked down. If I used ping.exe against it, you'd think it's offline.
PS C:\> ping X.X.X.X Pinging X.X.X.X with 32 bytes of data: Request timed out. Request timed out. Request timed out. Request timed out. Ping statistics for X.X.X.X: Packets: Sent = 4, Received = 0, Lost = 4 (100% loss),
I know this virtual machine is online—it's just blocking ICMP. How else can we test network connectivity? How about using the PowerShell version of ping, the Test-Connection cmdlet? In my case, I get an obscure error message "error due to lack of resources." This is because I'm pinging an IP address, and Windows Management Instrumentation (WMI) isn't available on this remote computer.
However, when attempting to use Test-Connection against google.com, for example, all looks well.
PS C:\> Test-Connection -ComputerName google.com Source Destination IPV4Address IPV6Address Bytes ------ ----------- ----------- ----------- ----- MACWINVM www.google.com 126.96.36.199 32 MACWINVM www.google.com 188.8.131.52 32 MACWINVM www.google.com 184.108.40.206 32 MACWINVM www.google.com 220.127.116.11 32
The Test-Connection cmdlet has a few different parameters you can use to tailor your query to your liking. For example, you can change the buffer size and define the amount of seconds between the pings. The output is the same, but the request is a little different.
Test-Connection www.google.com -Count 2 -BufferSize 128 -Delay 3
Test-Connection also can reach out to remote computers and ping a remote computer as well. Granted, you will need to have access to the remote computers to do this.
Test-Connection -Source 'SRV2', 'SRV1' -ComputerName 'www.google.com' Source Destination IPV4Address IPV6Address Bytes Time(ms) ------ ----------- ----------- ----------- ----- -------- SRV2 google.com 18.104.22.168 32 5 SRV2 google.com 22.214.171.124 32 5 SRV2 google.com 126.96.36.199 32 6 SRV2 google.com 188.8.131.52 32 5 SRV1 google.com 184.108.40.206 32 5 SRV1 google.com 220.127.116.11 32 5 SRV1 google.com 18.104.22.168 32 5 SRV1 google.com 22.214.171.124 32 5
Is the output too much, and you're just looking for a binary yes or no? No problem—just use the -Quiet parameter.
PS> Test-Connection -ComputerName google.com -Quiet True
PowerShell also has another excellent command to test network connectivity called Test-NetConnection. Test-NetConnection is the successor to Test-Connection and provides a lot of different ways to check network connectivity from a lot of various angles.
At its basic level, it merely needs a value for the -ComputerName parameter.
PS C:\> Test-NetConnection -ComputerName www.google.com ComputerName : www.google.com RemoteAddress : 126.96.36.199 InterfaceAlias : Ethernet 2 SourceAddress : 192.168.86.26 PingSucceeded : True PingReplyDetails (RTT) : 54 ms
Unlike Test-Connection though, Test-NetConnection can test whether ports are open. In the example below, I'm checking to see if port 80 is open. As expected (since I'm testing google.com), port 80 is open, since TcpTestSucceded equals True.
PS C:\> Test-NetConnection -ComputerName www.google.com -Port 80 ComputerName : google.com RemoteAddress : 188.8.131.52 RemotePort : 80 InterfaceAlias : Ethernet 2 SourceAddress : 192.168.86.26 TcpTestSucceeded : True
Remember tracert? We don't need that old-school utility anymore either. We now have the -TraceRoute parameter on Test-NetConnection!
Subscribe to 4sysops newsletter!
PS C:\> Test-NetConnection -ComputerName google.com -TraceRoute ComputerName : google.com RemoteAddress : 184.108.40.206 InterfaceAlias : Ethernet 2 SourceAddress : 192.168.86.26 PingSucceeded : True PingReplyDetails (RTT) : 44 ms TraceRoute : 192.168.86.1 192.168.0.1 220.127.116.11 18.104.22.168 22.214.171.124 126.96.36.199 188.8.131.52 184.108.40.206 220.127.116.11 18.104.22.168 22.214.171.124 126.96.36.199 188.8.131.52 184.108.40.206
Test-NetConnection has quite a few other parameters that allow you to test many different situations. If you had to pick only one command to do this, pick Test-NetConnection. Sometimes it won't be available if you're using an old version of PowerShell, but it is more feature rich and allows more control over how to craft requests to remote computers than Test-Connection.