
close
close
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 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.
Using the Test-Connection cmdlet in Windows PowerShell. (Image Credit: Jeff Hicks)
advertisment
test-connection chi-core01 -Count 2
The output has been formatted for your viewing pleasure. If you pipe a connection result to Get-Member, then you’ll discover the actual property names.
test-connection chi-core01 -Count 1 | Select Address,IPv4Address,ResponseTime,BufferSize
Discovering property names for our test connection. (Image Credit: Jeff Hicks)
advertisment
$computers = "chi-dc01","chi-dc02","chi-dc04"
test-connection $computers -Count 1 | Select Address,IPv4Address,ResponseTime,BufferSize
Specifying multiple computer names with the Test-Connection cmdlet in Windows PowerShell. (Image Credit: Jeff Hicks)
Using the Test-Connection cmdlet to test a connection with the -quiet parameter. (Image Credit: Jeff Hicks)
get-adcomputer -filter * -server chi-dc04 -SearchBase "CN=Computers,DC=Globomantics,DC=Local" |
Where { Test-Connection $_.name -count 1 -quiet } |
Select @{Name="Computername";Expression={$_.Name}}
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.
I could continue adding to the pipelined expression any cmdlet that accepts pipeline input by property name for Computername.
Or you might try something like this:
advertisment
$computers = "chi-dc01","chi-dc02","chi-dc04","chi-app01","chi-core01","chi-fp01","chi-fp02"
$computers | group {test-connection -count 1 -ComputerName $_ -quiet}
Using the Group-Object cmdlet to organize results in Windows PowerShell. (Image Credit: Jeff Hicks)
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.
The Test-NetConnection cmdlet in Windows PowerShell. (Image Credit: Jeff Hicks)
The Test-NetConnection cmdlet in PowerShell has failed because the host isn’t configured to respond to pings. (Image Credit: Jeff Hicks)
test-netconnection petri.com -CommonTCPPort HTTP
Using the Test-NetConnection cmdlet in PowerShell to test for connectivity to common ports. (Image Credit: Jeff Hicks)
Using the Test-NetConnection cmdlet in PowerShell to test connectivity for RDP. (Image Credit: Jeff Hicks)
Performing a trace route with the Test-NetConnection cmdlet in Windows PowerShell. (Image Credit: Jeff Hicks)
The trace route was not successful. (Image Credit: Jeff Hicks)
PS C:\> test-netconnection 172.16.10.100 -CommonTCPPort SMB -InformationLevel Quiet True
I tested to see if my NAS server is available. Here’s another example: I want to see which servers do not have Remote Desktop enabled.
"chi-dc01","chi-dc02","chi-dc04" | where { -NOT (Test-Netconnection $_ -CommonTCPPort RDP -InformationLevel Quiet)}
Using the Test-NetConnection cmdlet in PowerShell to determine which servers that don’t have Remote Desktop enabled. (Image Credit: Jeff Hicks)
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)
The command has failed. (Image Credit: Jeff Hicks)
Testing with the Test-WSMan cmdlet. (Image Credit: Jeff Hicks)
#requires -version 3.0 Function Test-PSRemoting { [cmdletbinding()] Param( [Parameter(Position=0,Mandatory,HelpMessage = "Enter a computername",ValueFromPipeline)] [ValidateNotNullorEmpty()] [string]$Computername, [System.Management.Automation.Credential()]$Credential = [System.Management.Automation.PSCredential]::Empty ) Begin { Write-Verbose -Message "Starting $($MyInvocation.Mycommand)" } #begin Process { Write-Verbose -Message "Testing $computername" Try { $r = Test-WSMan -ComputerName $Computername -Credential $Credential -Authentication Default -ErrorAction Stop $True } Catch { Write-Verbose $_.Exception.Message $False } } #Process End { Write-Verbose -Message "Ending $($MyInvocation.Mycommand)" } #end } #close function
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.
More in PowerShell
Microsoft’s New PowerShell Crescendo Tool Facilitates Native Command-Line Wraps
Mar 21, 2022 | Rabia Noureen
Most popular on petri