Search code examples
windowsbatch-filenetwork-programmingwifischeduled-tasks

Automatically connect to wifi network when no internet detected


EDIT: Found a fix without using Task Scheduler.

@echo off
:loop
set addresses=192.168.1.1
for %%a in (%addresses%) do ping %%a -n 1 > nul || netsh wlan add profile filename=C:\User\path\to\wireless\xml\profile.xml user=all
timeout /t 5
goto :loop

it repeats every 5 seconds


I am halfway there but have become stuck. I already have a batch file that can connect the laptop back to the network when manually run but wanted it to run when no internet is detected. So I went ahead and bound it to Task Scheduler with the Trigger being the "Microsoft-Windows-NetworkProfile/Operational" with an ID of 10001 (which is the Disconnect ID) but have no luck with it.

I believe it only triggers when it manually gets disconnected from the internet, not when the wireless card drops the signal on its' own. Any ideas as to what I can do?

OS: Windows 8.1

Wireless Interface: Cable Matters AC600 Dual Band Wireless dongle (doesn't allow Auto Connect to SSID and keeps dropping the connection for unknown reasons)


Solution

  • Found a solution!

    @echo off
    :loop
    set addresses=192.168.1.1
    for %%a in (%addresses%) do ping %%a -n 1 > nul || netsh wlan add profile filename=C:\User\path\to\wireless\xml\profile.xml user=all
    timeout /t 5
    goto :loop
    

    What this does is ping an address once (in this case an internal address so I don't get DDoS threats from sites) and if it responds back with an error, it will load a wifi profile. To get the profile first you will need to type

    netsh wlan show profiles
    

    to see what profiles you have (it only loads profiles for the wireless card currently in use). After seeing the names of the profiles you can download the profile by typing

    netsh wlan export profile name=[profile name]
    

    I believe it will save to the desktop and from there you can change the code listed at the top of this comment to match the filepath of the xml file to suit your needs.

    Cheers!