Improvements in Test-Connection
In Windows PowerShell, you could use the Test-Connection
cmdlet as a replacement for the Win32 console command, ping.exe
. One advantage of using the cmdlet was that the cmdlet returns objects that you can use more easily in scripting. You can use string hacking and regular expressions to extract the same data from the output of ping.exe
, but that is a lot more work and results in scripts that are harder to read.
With Windows PowerShell 5.1, the Test-Connection
command makes use of WMI. The command returns objects of the type System.Management.ManagementObject#root\cimv2\Win32_PingStatus
. With PowerShell 7.1, the command no longer depends on WMI and returns objects of the type Microsoft.PowerShell.Commands.TestConnectionCommand+PingStatus
. As a result of this change of object type, property names returned in PowerShell 7.1 differ from the properties returned in Windows PowerShell. Scripts that made use of some properties may not work properly without adjustment, in PowerShell 7, but that should not be a common issue.
Getting ready
You run this recipe on SRV1
after you have installed PowerShell 7 and created a console profile file.
How to do it...
- Using
Test-Connection
with the-Target
parameterTest-Connection -TargetName www.packt.com -Count 1
- Using
Test-Connection
with an IPv4 addressTest-Connection -TargetName www.packt.com -Count 1 -IPv4
- Using
Resolve-DnsName
to resolve the destination address$IPs = (Resolve-DnsName -Name Dns.Google -Type A).IPAddress $IPs |   Test-Connection -Count 1 -ResolveDestination
- Resolving the destination and performing a traceroute
Test-Connection -TargetName 8.8.8.8 -ResolveDestination -Traceroute |   Where-Object Ping -eq 1
- Using infinite ping and stopping with Ctrl-C
Test-Connection -TargetName www.reskit.net -Repeat
- Checking the speed of
Test-Connection
in PowerShell 7Measure-Command -Expression {test-connection 8.8.8.8 -count 1}
- Checking the speed of
Test-Connection
in Windows PowerShell$Session = New-PSSession -UseWindowsPowerShell Invoke-Command -Session $Session -Scriptblock { Measure-Command -Expression { Test-Connection -ComputerName 8.8.8.8 -Count 1} }
How it works…
In step 1, you test the connection between SRV1
and our publisher's online website. The output of this command looks like this:
Figure 2.30: Using Test-Connection with the -TargetName parameter
If you have a computer with a working IPv6 address, Test-Connection
prefers using IPv6, by default, as shown in the output from step 1. Should you want to test the IPv4 connection specifically, you can specify the -IPv4
switch explicitly, as shown in step 2:
Figure 2.31: Using Test-Connection with an IPv4 address
In step 3, you use Resolve-DnsName
cmdlet to determine the IPv4 address(es) for Dns.Google
. This site is Google's free DNS service, which Google offers via two well-known IPv4 addresses (8.8.8.8 and 8.8.4.4), which you can see in the output from this step:
Figure 2.32: Using Resolve-DnsName to resolve the destination address
The tracert.exe
Win32 console application allows you to trace the route between your host system and some external host. In step 4, you use Test-Connection
to trace the route between SRV1
and the computer at 8.8.8.8
. The output of this step looks like this:
Figure 2.33: Resolving the destination and trace route
With the ping.exe
Win32 console application, you can specify the -t
parameter to ping the target host continuously, which you can then stop by entering Ctrl-C. With Test-Connection
in PowerShell 7, you can now use the -Repeat
parameter to achieve the same outcome (and stop the test using Ctrl-C). You can see this in the output of step 5:
Figure 2.34: Using infinite ping and stopping with Ctrl-C
In steps 6 and 7, you compare the speed of Test-Connection
in Windows PowerShell and PowerShell 7. Running Test-Connection
in PowerShell 7 looks like this:
Figure 2.35: Checking the speed of Test-Connection in PowerShell 7
In Windows PowerShell 5.1, the output is similar (although slower) and looks like this:
Figure 2.36: Checking the speed of Test-Connection in Windows PowerShell 5.1
There's more...
In the output from step 1, you can see that the results are formatted differently from Windows PowerShell, with improvements to the output.
Step 2 shows how, in PowerShell 7, you can use the -IPv4
switch to use IPv4 explicitly. Similarly, if you want to use IPv6 specifically, you can use the -IPv6
switch. Neither switch was available with Windows PowerShell.
In step 3, you determine the IP addresses for the host Dns.Google
(8.8.8.8 and 8.8.4.4), which you then ping successfully. Note that this step both resolves the IP addresses into hostnames and performs the pings against each IP address. Google runs a free DNS service available to anyone on the internet. You can find out more at https://developers.google.com/speed/public-dns.
In steps 6 and 7, you compare the speed of the Test-Connection
command between Windows PowerShell 5.1 and PowerShell 7.1, As you can see in the output from these steps, the command is considerably faster in PowerShell 7.