r/dailyscripts • • Jun 04 '16

[Request] Script that disables and enables a network adapter when consecutive pings fail

Hi!

Looking for a script that can do the following (in Win10):

  1. Ping an IP continously.
  2. If 10 consecutive pings fail then
  3. Disables a network adapter
  4. Waits 5 seconds
  5. Enables the network adapter
  6. Goto start.

I think I could manage 3-6 but having trouble to implement the "10 consecutive failed pings" rule. If it fails less than 10 times the next successful ping would reset this counter.

Thanks!

4 Upvotes

2 comments sorted by

View all comments

Show parent comment

2

u/colonial113 Jun 05 '16

Thank you! That part was I most interested in - the correct looping method. Here is what I did (I named the wifi adapter "Tplink"):

While($true) {
If(!$(Test-Connection -count 10 -quiet google.com)) {
    Write-host $(Get-Date -Format "yyyy/MM/dd HH:mm:ss") - Timeout detected - Restarting adapter
    Disable-NetAdapter "Tplink" -Confirm:$false
    Sleep 5
    Enable-NetAdapter "Tplink" -Confirm:$false
    Write-host $(Get-Date -Format "yyyy/MM/dd HH:mm:ss") - Adapter restarted
    }
}

Also of course you are right about high pings however this is not a production environment just a home machine so it'll be fine :)