1 You can download the batch file using this link - MaintainInternetConnection.bat
- Windows security will warn you that it is not a commonly downloaded file. It will still download.
- Right-click on the downloaded file, select Properties and tick in the Unblock box so it is ready for use.
- I've called it MaintainInternetConnection.bat but you can rename it to whatever you want.
- You can see what is in it by right-clicking on it & selecting Edit. This is what is in it -Lines beginning with a pair of colons :: are explanatory remarks and do not do anything {they are similar to lines that can be started with REM but they do not appear when a batch file is running}.
Code:
:: This procedure continually tests for an internet connection using the same test that the TaskBar network icon ["Network Connection Status Icon", NCSI] uses - the ability to find www.msftconnecttest.com/connecttest.txt
Set OutputFolder=%Temp%
:DoTheTest
:: Test for internet connection
Set /a TestCounter=%TestCounter%+1
:: This internet connection test applies to Windows 10 Versions 1607 & later. For earlier Windows 10 Versions, and for Windows Server 2016, the relevant address would be http://www.msftncsi.com/ncsi.txt
:: Further details are in https://docs.microsoft.com/windows/privacy/manage-connections-from-windows-operating-system-components-to-microsoft-services#bkmk-ncsi
powershell -Command "Invoke-WebRequest http://www.msftconnecttest.com/connecttest.txt -OutFile '%OutputFolder%\TestResults.txt' "
dir "%OutputFolder%\TestResults.txt"
:: If there is an internet connection, the output file is created
:: If there is no internet connection, the output file is initially {momentarily} created but is deleted by powershell as it generates its failure response
:: If the txt file is found then there is an internet connection, otherwise there might not be. The test sometimes fails even though there is an internet connection. I have not seen this happen more than once in any repeated set of tests.
:: I have not seen any false test passes
:: Each connection test [i.e. the powershell and dir lines] takes 1.4-1.6 secs
If %ErrorLevel% EQU 0 (del "%OutputFolder%\TestResults.txt") & (GoTo AboutToRepeat)
:: If the test fails, it is repeated 7 times which takes about 10 secs as requested.
If %TestCounter% LSS 7 GoTo DoTheTest
:: If the test fails 7 times then disable and re-enable the "Wi-Fi 2" network adapter
netsh interface set interface "Wi-Fi 2" disabled
netsh interface set interface "Wi-Fi 2" enabled
:AboutToRepeat
:: An internet connection would normally be restored straightaway after re-enabling the adapter but a delay can be put in before re-testing just to avoid wasting time testing the connection while it is still sorting itself out
:: After a test is passed, having a delay before re-testing does no harm as long as it is not so long as to invalidate the rule of thumb that 7 tests = about 10 secs
:: So I chose a 1 secs delay for no particular reason other than balancing both these factors in a single command
TimeOut /T 1 /NoBreak
Set /a TestCounter=0
GoTo DoTheTest
:: Pause during testing
2 The batch file needs to be run as Admin and you can also, if you wish, make it run minimised & with a specific icon.
- Create a shortcut to it by right-clicking on it and selecting Create shortcut
- Set it up to run as Admin - right-click on the shortcut, select Properties, click on Advanced ... set the checkbox for Run as Admin, OK, OK. I sometimes find that shortcuts have the checkbox for Run as Admin greyed out for reasons that I have never understood. When this happens, I just delete the shortcut and start again.
- You can make it run minimised so that it is less obtrusive - right-click on the shortcut, select Properties, click in the Run field, select Minimised, OK.
- You can also select any icon you want so that you know what it is when you notice the icon in your TaskBar - right-click on the shortcut, select Properties, click on Change icon, select any icon you want, OK, OK.
- You can start the batch file by double-clickng on the shortcut and giving Admin permission to proceed.
3 The batch file keeps running all the time until you restart the computer at which stage you need to start it again. When you get used to it you will want it to run automatically. That is done using Task scheduler. See my article Make Task scheduler run a batch file minimised & with a specific icon - TenForums but make any comments here not there so I can see them in the context of this particular job.
- You really ought to take note of the advice in the article's annex about putting the batch file within a C:\Tools folder of your own making & with the access permissions I suggest in the article. I suggest using C:\Tools\Network for this one.
- I always need to open Task scheduler as Admin to be able to alter the necessary properties for a Task that itself will run as Admin
- You will need to include in the Task properties, General tab - Run with highest privileges
- The relevant Trigger to set for this is Begin the task - At log on
- When you get even more used to it, you might want it to run without displaying its icon in the TaskBar. That is a simple alteration. In the article's para 4.1, you will see that I suggestWshShell.Run chr(34) & "C:\Tools\ThisBatch\ThisBatch.lnk" & Chr(34), 7
and all you have to do is change the trailing 7 to a 0 so the line becomes
WshShell.Run chr(34) & "C:\Tools\ThisBatch\ThisBatch.lnk" & Chr(34), 0
4 Ask any questions you have now. Then, when you set it up in Task scheduler, you will have more questions so you can just post them here as well because I will remain subscribed to this thread.
Denis