Skip to content

Instantly share code, notes, and snippets.

@tejas-hosamani
Created December 23, 2019 06:20
Show Gist options
  • Save tejas-hosamani/91ed92acbb531cc8fcdb050e73d9d9ac to your computer and use it in GitHub Desktop.
Save tejas-hosamani/91ed92acbb531cc8fcdb050e73d9d9ac to your computer and use it in GitHub Desktop.
Monitor mobile WiFi and reconnect, if not available | Windows | Powershell
# 1) SSID of Wireless Network (CaSe Sensitive)
# netsh wlan show networks
$mySSID = 'Tejas'
# 2) Wireless Profile Name (CaSe Sensitive)
# netsh wlan show profile
$profileName = 'Tejas'
# 3) Name of Wireless Interface (CaSe Sensitive)
# netsh wlan show interfaces
$interfaceName = 'Wi-Fi'
$waitFor = 2
$timeoutIn = 2
$restartCount = 0
For($a=1; ; $a++) {
Write-Host "Running $a st/nd/rd/th time(s).."
Write-Host "Reconnected $restartCount st/nd/rd/th time(s).."
ping -n 1 google.com > NULL
if (!$?)
{
Write-Host "Restarting interface.."
netsh wlan disconnect interface="$interfaceName"
TIMEOUT $timeoutIn
netsh wlan connect ssid="$mySSID" Name="$profileName" Interface="$interfaceName"
TIMEOUT $timeoutIn
$restartCount++
}
Write-Host sleeping $waitFor sec
sleep $waitFor
cls
}
@tejas-hosamani
Copy link
Author

tejas-hosamani commented Dec 23, 2019

Credit: http://www.jeremyharlow.net/automatic-wi-fi-connectivity-checker-reconnect-script/
Original post was about making .bat file. I've made it for powerShell.

How to use:

  1. Create a new file with any name that you prefer, with .ps1 extention.
  2. Paste this code inside that file.
  3. Manually run first three commands, i.e:
# 1) SSID of Wireless Network (CaSe Sensitive)
netsh wlan show networks


# 2) Wireless Profile Name (CaSe Sensitive)
netsh wlan show profile


# 3) Name of Wireless Interface (CaSe Sensitive)
netsh wlan show interfaces
  1. ...And fill the variables($mySSID, $profileName and $interfaceName) respectively at line number 3, 7 and 11 inside script. And save the file.
  2. Finally run the file on Powershell every-time you want to "manage" it, with
    C:\> /path/to/file/<file_name>.ps1

That's it.

If you open powerShell inside the same directory as that of this file:
C:\> ./<file_name>.ps1

Example: ./pingNow.ps1

Hope it's useful 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment