How To Use PowerShell’s Test-Connection And Test-NetConnection Cmdlets
Think you can do better? Show us!
Pinging machines is a basic command for any ‘IT Pro’. Of course, this can be done in PowerShell, and in this Snip, we have a look at the various ways of doing it and some of the goodness that PowerShell can bring using the Test-Connection and Test-NetConnection cmdlets.
this snippet, we're going to explore the various different ways and options of pinging machines within PowerShell 1 very simple way of pinging a target in powershell is to use invoke expression and call the ping dot EXE command with a target on the end, as you can see it gives the standard sort of set of ping results back again. However, in powershell. We have a native commandment called test connection, which works at a very similar sort of way to ping. So, there, you go, we've got the source on the left hand side destination and the latency time. Test connection, however, has a set of switches against it, which we can use. Like the counts, switch which is the number of ping attempts to send to the target. The buffer size that we want to send in this case, 128 and the delay between the attempts. So we run that. So you see that we've got 5 ping attempts at 128 bytes and there was a 3 second delay between each one another useful switch within test connection is the source switch which means you can. This data set of machines, which want to ping from and it doesn't have to come from the local host so we run. This against google.com you will see is pinging from test VM 01, which is the localhost test VM 2. And test VM 3. And if we don't want to see all of this information. We can simply put, the quiet switch on the end, which will just reply a true or false depending whether the machine replies or not. There we go got a true this is really useful, but you want to do an if statement to see whether machine is online or not. With test connection we can also use the As job Switch, which this will just run the test connection. Ping attempts as a background job so if we run that you will see that immediately comes back to the command prompt. However, if we do a good job and pipe this into receive job it will get that job and receive the responses from it. This can be really useful if you've got a lot of machines to ping. Another native command, not within powershell is test net connection, which does a very similar job. Results come back in a slightly different format, but it's basically the same. However, with test net connection you can ping the target on a particular port in this case port eighty to google.com. There you go google.com on port 80 is responding. However, if we change that to Port 81. Run it again. We've seen it failed. Also with test net connection you can do it write route to the target. And it responds with a similar some set of results with all the hops along the way in the IP address for them. Again, with test net connection, we can run the information level switch of quiet and this will respond a true or false brilliant variable of whether the machine is online or not in this case is true. Every take the output from test net connection and pipe it into select star or see all of the results that comeback and all of the information. As you can see, there's quite a lot of information will come back here. Another option within powershell is to use get to WMI object of the class win 32 ping status. And if we filter that down to address equals google.com with google.com in quotes because similar sort of set of results come back. However, with this we can use multiple targets within one command line. There you go, we got a reply back from google.com and yahoo.com with this method, we can also filter it using a buffer size of 128, so this will send 128 bytes to the target. So we look at the results on the right hand side says bytes of 128. And again if we pipe all of this into select star. We will see the amounts of data that comes back again is quite a sizable amount of data. And that's how to ping with powershell.