0

Greeting, fellows. I discovering new world of PowerShell, and while playing with it, found some unclear behavior of Test-Connection cmdlet. The goal was to get fastest ping reply check from address. Any IP as (216.58.215.78) or domain name as (google.com) used in scripts runs quite fast, but when used 192.168.1.1, router adress, have delay in one scenario.

  1. Using method in if statement. Fast execution (around 40 lines per second)

while($true){ if(Test-Connection -ComputerName "192.168.1.1" -Count 1){ "ok" } }

https://i.stack.imgur.com/msODA.png

  1. No using if statement. Slow execution (around 5 seconds for single line)

while($true){ Test-Connection -ComputerName "192.168.1.1" -Count 1 }

https://i.stack.imgur.com/VT8Vj.png

First script can prompt (and get answers from Router IP, which confirmed with Wireshark, about dozens of times per minute). Second, for some reasons, can prompt result from test much less often.[enter image description here][1] I would be grateful for any hint.

P.S. If i specify localhost ip, script fly like the second first, around 40 prompts in second.

while($true){ Test-Connection -ComputerName "127.0.0.1" -Count 1 }

S1lentFox
  • 1
  • 3
  • Slowness could be the reverse DNS lookup of IP 192.168.1.1 that never resolves to a name. – AdminOfThings Sep 23 '20 at 18:47
  • Thanks for idea. I configured C:\Windows\System32\drivers\etc\hosts, and added string: 192.168.1.1 Router ..... Script runs without delay. Anyways, can't understand why wrapping in"if" statement change execution, and what exactly it changing... – S1lentFox Sep 23 '20 at 19:12
  • Yeah, @AdminOfThings is spot on - the reason the delay is _not_ incurred in the `if()` condition is that it's not actually `Test-Connection` performing the reverse dns lookup - that comes from the formatting data associated with the resulting object. `Get-FormatData 'System.Management.ManagementObject#root\cimv2\Win32_PingStatus'` – Mathias R. Jessen Sep 23 '20 at 19:16
  • @Mathias R. Jessen, thanks for hint! – S1lentFox Sep 23 '20 at 19:19

0 Answers0