I have some trouble to do a script for Windows 10 that will check my internet connection every 2 minutes and restart the computer if it doesn’t find any connection.
I tried with PowerShell:
mainLoop()
sleep 120
{
if (!(Test-Connection 8.8.8.8 -Quiet)) {
#Write-Host "Not connected"
Restart-Computer
} else {
mainLoop()
}
}
Do you have some advice to make it works?
Loop while the remote host is reachable, and restart once the loop terminates:
while (Test-Connection 8.8.8.8 -Quiet) {
Start-Sleep -Seconds 120
}
Restart-Computer
Well – you’re not looping for one.
Call your function in a
while($true)
loop and sleep after testing the connection if it’s still connected, for starters.Restart-Computer
is somewhat drastic in response to a loss of connection — isn’t it?I would
ping 192.168.1.1
first to check if it’s not your router/modem needing a reboot. Also have a look atnetsh winsock reset
andipconfig
options for a faster/softer fix.