Notice: This post is a part of the PowerShell Monday series — a group of quick and easy to read mini lessons that briefly cover beginning and intermediate PowerShell topics. As a PowerShell enthusiast, this seemed like a beneficial way to ensure those around me at work were consistently learning new things about Windows PowerShell. At some point, I decided I would share these posts here, as well. Here’s the PowerShell Monday Table of Contents.
Let’s spend a small part of this Monday morning learning about the Test-NetConnection cmdlet — a potential, telnet replacement. This cmdlet was introduced in PowerShell 4.0 which shipped “in box” with Windows 8.1 and Server 2012 R2.
Test-NetConnection returns a connectivity test to a remote computer. When the cmdlet is entered without a specific computer in which to test against, it will use internetbeacon.msedge.net, a domain owned by the Microsoft Corporation.
Test-NetConnection ComputerName : internetbeacon.msedge.net RemoteAddress : 126.96.36.199 InterfaceAlias : Ethernet SourceAddress : 10.10.10.50 PingSucceeded : True PingReplyDetails (RTT) : 38 ms
This can be helpful, and provides proof of Internet connectivity, but often you’ll want to test a connection closer to the computer that initiated the test. In the case where you want to check a computer in a data center, or only a room away, you can use the -ComputerName parameter and provide a computer name as the parameter value, such as DC01 in this example.
Test-NetConnection -ComputerName DC01 ComputerName : DC01 RemoteAddress : 10.10.20.2 InterfaceAlias : Ethernet SourceAddress : 10.10.10.50 PingSucceeded : True PingReplyDetails (RTT) : 39 ms
There’s a port parameter that can be used, that will allow you to test against a specific port on the remote computer.
Test-NetConnection -ComputerName DC01 -Port 5985 ComputerName : DC01 RemoteAddress : 10.10.10.2 RemotePort : 5985 InterfaceAlias : Ethernet SourceAddress : 10.10.10.50 PingSucceeded : True PingReplyDetails (RTT) : 42 ms TcpTestSucceeded : True
If you don’t know the port number, you may be able to use the -CommonTCPPort parameter and supply one of the accepted parameter values. There’s directions at the bottom of today’s PSMonday on how to view the help for this cmdlet, that indicate the accepted values. In this next example, we use RDP as the parameter value for this -CommonTCPPort parameter.
Test-NetConnection -ComputerName DC01 -CommonTCPPort RDP ComputerName : DC01 RemoteAddress : 10.10.10.2 RemotePort : 3389 InterfaceAlias : Ethernet SourceAddress : 10.10.10.50 PingSucceeded : True PingReplyDetails (RTT) : 45 ms TcpTestSucceeded : True
There’s also an -InformationLevel parameter that will allow you to specify that you want detailed information returned, or in the case of the example below, only want a Boolean (true or false), value returned.
Test-NetConnection -ComputerName DC01 -CommonTCPPort HTTP -InformationLevel Quiet False
There are more features to Test-NetConnection, to include a -TraceRoute and a -Hops parameter. If you’re interested in learning more about those, or want to read the full help — much of which you already know, now — enter one of the next two commands.
Get-Help -Name Test-NetConnection -Full Get-Help -Name Test-NetConnection -ShowWindow
Next time you need to telnet from one host to another, try Test-NetConnection instead of telnet, and see if it isn’t a little more helpful.