Skip to content

Instantly share code, notes, and snippets.

@flevi29
Created February 5, 2021 13:39
Show Gist options
  • Save flevi29/ac6dbbedd21d9acf13b4e53e798e8110 to your computer and use it in GitHub Desktop.
Save flevi29/ac6dbbedd21d9acf13b4e53e798e8110 to your computer and use it in GitHub Desktop.
When internet connection is lost, or a certain amount of seconds passed, it shuts down the computer.
@echo off
title Automated shutdown
:: How many seconds*10 until power off ; 360 : 1 hour, < 1 : never,
set "tenSeconds=360"
if %tenSeconds% leq 0 (goto :ELSE)
set /a seconds="tenSeconds * 10"
echo|set /p="Waiting for internet connection to be lost or %seconds% seconds to then shut down . . ." & echo. & echo.
set "remaining= seconds ~ remaining . . ."
powershell -Command "$remainingstr = '%remaining%'; $amount = ($('%seconds%').Length + $remainingstr.Length); $back = \"$(\"`b\"*$amount)$(' '*$amount)$(\"`b\"*$amount)\"; $offLineCount = 0; $tpassed = 0; $StartDate = (Get-Date); do { Write-Host -NoNewLine \"$back\"; Write-Host -NoNewLine \"$([int][Math]::Floor(%seconds% - $tpassed))$remainingstr\"; Start-Sleep -s 10; $tpassed = (NEW-TIMESPAN -Start $StartDate -End $(Get-Date)).TotalSeconds; if (!(Get-NetRoute | ? DestinationPrefix -eq '0.0.0.0/0' | Get-NetIPInterface | Where ConnectionState -eq 'Connected')) { $offLineCount++; } else { $offLineCount = 0; } } while ($offLineCount -ne 6 -and $tpassed -lt %seconds%); Write-Host -NoNewLine \"$back\"; exit 0; "
goto :ENDIF
:ELSE
echo|set /p="Waiting for internet connection to be lost to then shut down . . ." & echo. & echo.
powershell -Command "$offLineCount = 0; do { Start-Sleep -s 10; if (!(Get-NetRoute | ? DestinationPrefix -eq '0.0.0.0/0' | Get-NetIPInterface | Where ConnectionState -eq 'Connected')) { $offLineCount++; } else { $offLineCount = 0; } } while ($offLineCount -ne 6); exit 0; "
:ENDIF
shutdown /s /t 60
echo|set /p="Shutting down . . ." & echo. & echo.
echo|set /p="To abort type: shutdown /a" & echo. & echo.
cmd /k
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment