Skip to content

Instantly share code, notes, and snippets.

@desaster
Created April 15, 2018 16:30
Show Gist options
  • Save desaster/1739f531a65f74917993d676ffa1ca7a to your computer and use it in GitHub Desktop.
Save desaster/1739f531a65f74917993d676ffa1ca7a to your computer and use it in GitHub Desktop.
#
# This powershell script automates the task of restarting a network
# adapter on a particular System event.
#
# The practical application is to work around the issue of
# buggy Killer Wifi (Qualcomm Atheros) cards crashing during standby.
#
# Tested on XPS 13 9370 (2018)
#
# The predefined trigger checks for the following event:
# LogName: System
# Source: Qcamain10x64
# EventID: 5002
# Before using this script, verify that you are getting the same
# events in your Event Viewer.
#
# Optionally install the BurntToast powershell module to get nice
# toast notifications when triggered:
# https://www.powershellgallery.com/packages/BurntToast/
#
# Usage:
#
# -Add Register scheduled task
# -Remove Unregister scheduled task
# -RestartAdapter Restart wifi (this is used by the scheduled task)
#
param(
[switch]$Add,
[switch]$Remove,
[switch]$RestartAdapter
)
# change this to match your wifi adapter name
$KW_AdapterName = "Wi-Fi"
# Name which the new task will be called in Task Scheduler
$KW_TaskName = "KillerWifiRestart"
if ($Add) {
Write-Host "Adding Scheduled Task"
$xml = @"
<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
<RegistrationInfo>
<Description>Restart network adapter on error event</Description>
</RegistrationInfo>
<Triggers>
<EventTrigger>
<ExecutionTimeLimit>PT5M</ExecutionTimeLimit>
<Enabled>true</Enabled>
<Subscription>&lt;QueryList&gt;&lt;Query Id="0" Path="System"&gt;&lt;Select Path="System"&gt;*[System[Provider[@Name='Qcamain10x64'] and EventID=5002]]&lt;/Select&gt;&lt;/Query&gt;&lt;/QueryList&gt;</Subscription>
</EventTrigger>
</Triggers>
<Principals>
<Principal id="Author">
<RunLevel>HighestAvailable</RunLevel>
</Principal>
</Principals>
<Settings>
<MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
<DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
<StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
<AllowHardTerminate>true</AllowHardTerminate>
<StartWhenAvailable>true</StartWhenAvailable>
<RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
<IdleSettings>
<StopOnIdleEnd>true</StopOnIdleEnd>
<RestartOnIdle>false</RestartOnIdle>
</IdleSettings>
<AllowStartOnDemand>true</AllowStartOnDemand>
<Enabled>true</Enabled>
<Hidden>false</Hidden>
<RunOnlyIfIdle>false</RunOnlyIfIdle>
<WakeToRun>false</WakeToRun>
<ExecutionTimeLimit>PT5M</ExecutionTimeLimit>
<Priority>7</Priority>
</Settings>
<Actions Context="Author">
<Exec>
<Command>C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe</Command>
<Arguments>-NoProfile -WindowStyle Hidden -File $PSCommandPath -RestartAdapter</Arguments>
</Exec>
</Actions>
</Task>
"@
Register-ScheduledTask -Xml $xml -TaskName $KW_TaskName
} elseif ($Remove) {
Write-Host "Removing Scheduled Task"
Get-ScheduledTask -TaskName $KW_TaskName | Unregister-ScheduledTask -Confirm:$false
} elseif ($RestartAdapter) {
Write-Host "Restarting Wi-Fi Adapter"
if (Get-Module -ListAvailable -Name "BurntToast") {
Import-Module BurntToast
New-BurntToastNotification -Text "Network adapter failure detected", "Restarting network adapter: $KW_AdapterName"
}
Restart-NetAdapter $KW_AdapterName -Confirm:$false
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment