If you look up how a ping can be done endlessly with Test-Connection, you will usually find solution with a while loop or something else complicated. The dilemma is that ping provides the -t parameter, but Test-Connection only provides the Count parameter and this parameter requires a value and there is no endless parameter. It’s cumbersome to enter a high value every time the command is executed. Why not use the int32 MaxValue field to provide a high value so that the ping is running endlessly?
Here we go.
Int32::MaxValue
Test-Connection sid-500.com -Count ([int32]::MaxValue)
Okay, I can already hear your words: “Hey, hold on. It’s even more complicated now, now I’m supposed to enter these cryptic characters?”
Read on. Now we’ll make a small but fine function out of it. I will call it Test-Endless.
Test-Endless
function Test-Endless { [CmdletBinding()] param ( [Parameter(Mandatory=$true)] $Target ) Test-Connection $Target -Count ([int32]::MaxValue) }
Feel free to omit the target parameter.
How to use it
Copy the code into your PowerShell ISE session and run the code. Then type the command and have fun with it.
If you want to make the function permanently available, so that the function is there every time you start PowerShell, you have to create a folder in C:\Program Files\WindowsPowerShell\Modules. Name it Test-Endless. Then save the code as a .psm1 file in that folder. The screenshot below should be a help.
See you next time with PowerShell.
Categories: PowerShell, Windows 10, Windows Server
What if I want to test the connection every 10 seconds endless?
LikeLike
Hi,
Possible to add time & date on the output?
interesting to know when pinging a server and know what time/date he was down.
thanks,
LikeLike
filter timestamp {“$(Get-Date -Format G): $_”}
Should get you the Date + Time
LikeLike