Skip to content

Instantly share code, notes, and snippets.

@agamm
Last active October 13, 2022 22:01
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 agamm/c9feeca2d4df5a0e42417a3a2c1b988a to your computer and use it in GitHub Desktop.
Save agamm/c9feeca2d4df5a0e42417a3a2c1b988a to your computer and use it in GitHub Desktop.
Reset internet when down on Windows via powershell
// unzip.dev ?
if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs; exit }
# Thanks: https://den.dev/blog/powershell-windows-notification/
function Show-Notification {
[cmdletbinding()]
Param (
[string]
$ToastTitle,
[string]
[parameter(ValueFromPipeline)]
$ToastText
)
[Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime] > $null
$Template = [Windows.UI.Notifications.ToastNotificationManager]::GetTemplateContent([Windows.UI.Notifications.ToastTemplateType]::ToastText02)
$RawXml = [xml] $Template.GetXml()
($RawXml.toast.visual.binding.text|where {$_.id -eq "1"}).AppendChild($RawXml.CreateTextNode($ToastTitle)) > $null
($RawXml.toast.visual.binding.text|where {$_.id -eq "2"}).AppendChild($RawXml.CreateTextNode($ToastText)) > $null
$SerializedXml = New-Object Windows.Data.Xml.Dom.XmlDocument
$SerializedXml.LoadXml($RawXml.OuterXml)
$Toast = [Windows.UI.Notifications.ToastNotification]::new($SerializedXml)
$Toast.Tag = "PowerShell"
$Toast.Group = "PowerShell"
$Toast.ExpirationTime = [DateTimeOffset]::Now.AddMinutes(1)
$Notifier = [Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier("PowerShell")
$Notifier.Show($Toast);
}
function Reset-Internet ($sleep) {
Show-Notification -ToastTitle "Resetting internet" -ToastText "Current $sleep."
if($sleep -lt 10) {
Write-Host "Hardcore reset"
netsh winsock reset
netsh int ip reset
ipconfig /renew
ipconfig /flushdns
}
netsh interface set interface "Wi-Fi" disabled
netsh interface set interface "Wi-Fi" enabled
}
$sleep = 10
do {
if (Test-Connection -ComputerName google.com -Quiet) {
Write-Host "You have internet sleeping for $sleep..."
sleep $sleep
$sleep *= 1.1
} else {
Write-Host "Resetting internet (sleep=$sleep)..."
Reset-Internet $sleep
$sleep /= 1.2
}
} while ($true)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment