Skip to content

Instantly share code, notes, and snippets.

@jcefoli
Last active January 27, 2024 09:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jcefoli/ae832e8172f52cf036343900736df9c4 to your computer and use it in GitHub Desktop.
Save jcefoli/ae832e8172f52cf036343900736df9c4 to your computer and use it in GitHub Desktop.
Windows Sandbox Connection Tester & Reboot
# This will ping google to test for a WAN connection and reboot if it fails
# Workaround to my Windows Sandbox always losing network
# The weird logging to write-host/write-output is designed to be used with Windows task scheduler (interactive logon) or via console directly
$host.ui.RawUI.WindowTitle = "Network Checker"
Clear-Host
$dt = (get-date).toString()
Write-Host "Network Check Running. Logfile: C:\Users\$($env:username)\networkCheck.log"
Write-Output "[$dt] Network Check Running. Logfile: C:\Users\$($env:username)\networkCheck.log" | Out-File -FilePath "C:\Users\$($env:username)\networkCheck.log" -Encoding utf8 -Append
Start-Sleep -Seconds 15 #If running as scheduled task, give Windows Sandbox some time to connect
while ($true) {
$result = Test-Connection -ComputerName google.com -Count 1
$dt = (get-date).toString()
if ($result.Status -eq "Success") {
Write-Host "[$dt] UP"
#Write-Output "[$dt] UP" | Out-File -FilePath "C:\Users\$($env:username)\networkCheck.log" -Encoding utf8 -Append
} else {
Write-Host "[$dt] ERR - No Network Connection. Rebooting."
Write-Output "[$dt] ERR - No Network Connection. Rebooting." | Out-File -FilePath "C:\Users\$($env:username)\networkCheck.log" -Encoding utf8 -Append
. shutdown -r -t 01
}
Start-Sleep -Seconds 60
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment