Test Network Connectivity with the PowerShell Test-Connection Cmdlet

Since the first time computers were networked together, people have had to test their connections. Just like a dial tone that tells you the phone is ready to use, many IT pros want to make sure they have a remote connection before beginning their ‘call’. For IT pros, this has traditionally meant using the PING utility that has existed since forever. It still works, and you can even use it in PowerShell. But when used in PowerShell, you get text output that’s hardly useful for scripting. So let me give you some additional options. Some of these cmdlets have changed through different PowerShell versions. I am testing from a Windows 8.1 desktop running PowerShell 4.0. If something doesn’t work for you, be sure to read cmdlet help and examples.

The PowerShell Test-Connection Cmdlet

The PING utility works by sending a special type of packet, an Internet Control Message Protocol echo request (ICMP), to a remote computer. Similar to the way a sonar pings another submarine, PING sends out a probe and waits for a response. The PowerShell equivalent is the Test-Connection cmdlet. This cmdlet is actually a wrapper for the Win32_PingStatus WMI class, but it’s easier to use because it’s a cmdlet. All you need to do is specify a remote computer name or IP address. The assumption is that you are testing a connection to another computer in your domain.

If you merely want to test a connection, then you should use the –Quiet parameter. Instead of response data, you get a simple true or false that indicates whether the computer responded to a ping request.

Using the Test-Connection cmdlet to test a connection with the -quiet parameter. (Image Credit: Jeff Hicks)

This command is getting all computer accounts in the default computers container and pinging each name once. If the computer responds, PowerShell passes the ADComputer object down the pipeline, where I’m simply writing an object with a single property of Computername.

Sponsored

I could continue adding to the pipelined expression any cmdlet that accepts pipeline input by property name for Computername.

Using the Group-Object cmdlet, I’ve organized my results based on whether they responded or not.

Using PowerShell’s Test-NetConnection cmdlet

The Test-Connection cmdlet is included out of the box in PowerShell. If you are running Windows 8 or later, then you probably have the NetTCPIP module, which includes another command called Test-NetConnection. This command will provide additional connection information.

Using the Test-NetConnection cmdlet in PowerShell to determine which servers that don’t have Remote Desktop enabled. (Image Credit: Jeff Hicks)

Using the Test-WSMan cmdlet in PowerShell

Most of the testing I’ve shown thus far involves ICMP pings, where the computer or firewall must be configured to allow. It is also possible that the network adapter will apply to a ping even though the operating system is hung. If you want to verify that the remote computer is ready for you, you can try using the Test-WSMan cmdlet. By this point I am assuming you have deployed PowerShell to your servers and enabled remoting. If not, you are making your life much more difficult. This cmdlet will verify that you can connect with the WSMan protocol, which means you can take advantage of the CIM cmdlets and remoting cmdlets like Invoke-Command.

Using the Test-WSMan cmdlet in PowerShell. (Image Credit: Jeff Hicks)

Here’s why this might be useful. The remote computer CHI-Win81 is up and running and replies to pings.

The command has failed. (Image Credit: Jeff Hicks)

But when I try to do something with it, the command fails because the WinRM service is not running on the client. It would have been better to try testing first.

Testing with the Test-WSMan cmdlet. (Image Credit: Jeff Hicks)

Unfortunately, Test-WSMan doesn’t have any parameters to provide a simple Boolean response.

Sponsored

As a result, I wrote a simple wrapper function.

PowerShell

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

#requires -version 3.0

FunctionTest-PSRemoting{

[cmdletbinding()]

Param(

[Parameter(Position=0,Mandatory,HelpMessage="Enter a computername",ValueFromPipeline)]

This command will give you a simple true or false result. If you use alternate credentials and get a false re-try with –Verbose so you can see the error message.

You now have a variety of testing tools that I encourage you to use if you are scripting a process that involves a large number of computers. As always be sure to read full help and examples for everything I’ve shown you.