Author Posts

July 21, 2016 at 12:27 pm

Hi, I'm PS noob. I am also a noob C++ programmer (I know the basics).

Is it possible to ping two servers indefinitely while only displaying/log ONLY when the latency is more than 100ms?

I have two monitors.
I'm gaming and sometime having bad connection to the game server.
The server IP will be either 103.28.54.1 or 103.10.124.1
currently I open two Command Prompt windows and do :

ping 103.28.54.1 -t
ping 103.10.124.1 -t

separately in each window

So I look regularly to the CMD windows to check if there are any latency issues.

It would be convenience if the output only display when latency is above certain threshold and would be nicer if I can play a WAV file when the threshold is reached so that I could stay away from the fight for a while if latency is bad 🙂

I looked into PowerShell and think it could solve this little problem. So I started learning it today 🙂
I tried some IF statements and few FOR loops. Doesn't work (doesn't know how to make it work).
Is possible to do that with the condition mentioned? How?

July 21, 2016 at 2:38 pm

Hi Areriff,

This seems like it may fit your needs:

$server = '8.8.8.8'
$responseThreshold = 0
$processName = 'notepad++'
$soundFile = 'c:\WINDOWS\Media\notify.wav'

$loopOn = $true

while ($loopOn -eq $true)
{
    $ping = Test-Connection -ComputerName $server -count 1 | Select-Object ResponseTime
    $timeStamp = (Get-Date).ToShortTimeString()
    Write-Output "$timeStamp : $($ping.responsetime) ms"
    
    #Check response time against threshold, play sound if greater than or equal to.
    if ($ping.ResponseTime -ge $responseThreshold)
    {
        $sound = New-Object System.Media.SoundPlayer
        $sound.SoundLocation = $soundFile
        $sound.Play()
    }
    
    #Check if process is still running, if not, exit loop.
    try { Get-Process -Name $processName -ErrorAction Stop | Out-Null}
    catch { $loopOn = $false }
    Start-Sleep 1
}

You can enter the site specific settings at the top including server address and response threshold (in ms).
I thought you probably want this to run only while your game is running, so it checks for the process, and exits the loop if it is not found. I used notepad++ for testing, so be sure to update that variable with the name of the game process. The datetime and response time will update in the console so you can see spikes and other activity if desired.

July 21, 2016 at 2:40 pm

This is a test.

Do {
Test-Connection google.com | Select-Object Address, 
IPV4Address, ResponseTime, ResponseTimeToLive | 
Where-Object {$_.ResponseTime -gt '100'}} While ($true)

July 21, 2016 at 3:14 pm

Trevor's code looks much better than this, but a quick bash :

$devices = 'www.yahoo.com', 'www.google.com'

$hash = @()
while($true){
    $devices | foreach {
        
        if ((Test-Connection $_ -count 2).ReponseTime -le 100) {
            $hash += @{ Normal= "$_ less than 100ms" } 
        }else{   
            $hash += @{ WARNING="$_ More than 100ms" } 
        }
            
            $Hash | foreach { [PSCustomObject]@{
                
                Status = $_.keys | % { "$_" }
                Device = $_.Values | % { "$_" }
                }
            } 
        }
    }

July 21, 2016 at 3:35 pm

Great, thanks. I'll study the suggestions and try it.