Orok
Orok

Reputation: 1

Automatically restart computer when internet connection is lost

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?

Upvotes: 0

Views: 1705

Answers (1)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200233

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

Upvotes: 3

Related Questions