How to automate resetting the adapter?

Page 1 of 4 123 ... LastLast

  1. Posts : 12
    Windows 10 64
       #1

    How to automate resetting the adapter?


    Hi,

    My Wifi connection periodically drops internet connection. It shows connected but no internet and resetting seems to fix it. I know I should find the real fix instead of a workaround but until then can someone tell me the list of commands to reset it when that happens?

    I already know how to disable/enable it using netsh but I would like to only do that after 10sec of no internet. Is there a way to do it by putting these commands in a batch file?

    Thanks.
      My Computer


  2. Posts : 15,444
    19043.1237
       #2

    Check the adapter properties, power management, uncheck box Allow the Computer to turn off to save power
      My Computer


  3. Posts : 12
    Windows 10 64
    Thread Starter
       #3

    I have already tried everything. I made a thread before here. The good news is it doesn't happen frequently after Windows update but it's still a problem.
      My Computer


  4. Posts : 11,352
    Windows 10 Home x64 Version 21H2 Build 19044.1706
       #4

    zhaider said:
    Is there a way to do it by putting these commands in a batch file?
    The reset batch file can just be [Run as Admin]
    DisableEnableWiFiCard.bat
    Code:
    netsh interface set interface "Wi-Fi" disabled
    netsh interface set interface "Wi-Fi" enabled
    where Wi-Fi is the name of the WiFi card in question as shown in Network & sharing centre, Change adapter settings

    The internet test can be based on the same test that your internet connectivity icon uses. I call my batch file -
    WWWConnectionTest.bat
    Code:
    :: Initialisation
    Set WWWConnection=Off
    Set OutputFolder=%Temp%
    Set /a TestCounter=0
    :DoTheTest
    :: Run test 3 times if connection appears to be off in case it was a duff result [seen at least once]
    Set /a TestCounter=%TestCounter%+1
    powershell -Command "Invoke-WebRequest http://www.msftconnecttest.com/connecttest.txt -OutFile '%OutputFolder%\TestResults.txt' "
    dir "%OutputFolder%\TestResults.txt"
    If %ErrorLevel% GTR 0 (Set WWWConnection=Off) & (If %TestCounter% LSS 3 GoTo DoTheTest)
    If %ErrorLevel% EQU 0 (Set WWWConnection=On) & (del "%OutputFolder%\TestResults.txt")
    Pause during testing - WWWConnection is %WWWConnection%
    :: Notes
    :: If there is an internet connection, the output file is created
    :: If there is no internet connection, the output file is initially created but is deleted by powershell when it generates its failure response
    and you could wrap this in a TimeOut loop to make it re-check every 10 secs instead of, as now, re-checking 3 times if it seems to be off
    Code:
    TimeOut /T 10 /NoBreak
    then use the value of %WWWConnection% to decide whether to run the commands in DisableEnableWiFiCard.bat

    Denis
    Last edited by Try3; 22 Oct 2019 at 21:06.
      My Computer


  5. Posts : 65
    Windows 10 Pro 1909 18363.959
       #5

    These commands will reset your network stack:

    ipconfig /release
    ipconfig /flushdns
    netsh windsock reset
    netsh interface ipv4 reset
    netsh interface ipv6 reset
    netsh windsock reset catalog
    netsh int ipv4 reset reset.log
    netsh int ipv6 reset reset.log

    Just copy and paste them into either a .cmd or .bat file, then you can create a shortcut on your desktop pointing to the .cmd or .bat file

    Then restart your computer and your network stack will re-initialize it self
      My Computers


  6. Posts : 12
    Windows 10 64
    Thread Starter
       #6

    Try3 said:
    The reset batch file can just be [Run as Admin]
    DisableEnableWiFiCard.bat
    Code:
    netsh interface set interface "Wi-Fi" disabled
    netsh interface set interface "Wi-Fi" enabled
    where Wi-Fi is the name of the WiFi card in question as shown in Network & sharing centre, Change adapter settings

    The internet test can be based on the same test that your internet connectivity icon uses
    WWWConnectionTest.bat
    Code:
    :: Initialisation
    Set WWWConnection=Off
    Set OutputFolder=%Temp%
    Set /a TestCounter=0
    :DoTheTest
    :: Run test 3 times if connection appears to be off in case it was a duff result [seen at least once]
    Set /a TestCounter=%TestCounter%+1
    powershell -Command "Invoke-WebRequest http://www.msftconnecttest.com/connecttest.txt -OutFile %OutputFolder%\TestResults.txt"
    dir %OutputFolder%\TestResults.txt
    If %ErrorLevel% GTR 0 (Set WWWConnection=Off) & (If %TestCounter% LSS 3 GoTo DoTheTest)
    If %ErrorLevel% EQU 0 (Set WWWConnection=On) & (del %OutputFolder%\TestResults.txt)
    :: Pause during testing - WWWConnection is %WWWConnection%
    :: Notes
    :: If there is an internet connection, the output file is created
    :: If there is no internet connection, the output file is initially created but is deleted by powershell when it generates its failure response
    and you could wrap this in a TimeOut loop to make it re-check every 10 secs instead of, as now, re-checking 3 times if it seems to be off
    Code:
    TimeOut /T 10 /NoBreak
    then use the value of %WWWConnection% to decide whether to run the commands in DisableEnableWiFiCard.bat

    Denis
    Thanks! Where can I find this WWWConnectionTest.bat? Also, if I understood it right the commands you posted seems to be setting WWWConnection to be off while I'm suppose to check it if it's off and then run the disable/enable commands.

    Also, please tell me how to wrap it in a timeout loop? Sorry, if it's obvious but I just want didn't understand how to bring this all together especially when the script you posted is generating a file every time it checks (or not?).
      My Computer


  7. Posts : 11,352
    Windows 10 Home x64 Version 21H2 Build 19044.1706
       #7

    WWWConnectionTest.bat is just a name that I've given to the code that is underneath that name. In other words, I was suggesting that that code should be saved as the file WWWConnectionTest.bat
    I have slightly altered the code in my first post so it now operates as a safe demonstrator.
    - I removed the :: from the start of one line so that you can now run it and it will stop at the end to tell you the current connection state.
    - I altered a couple of lines so it would run correctly even if your user name had a space in it. Your user name forms part of the folder path to %Temp% so this change was necessary yet I had not previously noticed the need for it.


    WWWConnection is a variable and is set to Off at the start of the code and changed, by the code, to On if the test passes.

    The code generates a text file if the test passes and then deletes it when it has finished with it.




    Check that the two netsh commands I use work in your circumstances. Wait for the problem to manifest itself then open an Admin command prompt and manually run each of these two commands using the correct name of your wifi card as shown in Network & sharing centre, Change adapter settings. Remember to enclose the name in quotation marks.
    Code:
    netsh interface set interface "Wi-Fi" disabled
    netsh interface set interface "Wi-Fi" enabled
    Question - are these the same commands as those you are currently using to get your adapter working again?

    While you are at it, check that there is nothing hindering the call to the powershell function. Whilst you are definitely online without any comms problems, open a command prompt [not a powershell window] and enter
    Code:
    powershell -Command "Invoke-WebRequest http://www.msftconnecttest.com/connecttest.txt -OutFile '%Temp%\TestResults.txt' "
    and then have a look in the %Temp% folder for that txt file - you can just enter %Temp% in File explorer's address bar to get there. There is nothing important about the contents of the txt file, it's whether or not there are any problems making it that matters. You should delete the text file when you have found it.
    - If this powershell command works then you can run the whole of the WWWConnectionTest.bat batch file and it will report
    Pause during testing - WWWConnection is On
    or
    Pause during testing - WWWConnection is Off
    as the case may be.


    If those two netsh commands do allow your internet connection to work again in your circumstances, and
    if that powershell command line works {and hence the WWWConnectionTest.bat batch file works}, and
    if you are really going to use this batch file
    but
    you do not have enough experience with writing batch files to do it yourself,
    then
    I will write out the whole thing for you. So tell me the correct name of your wifi card, confirm that the two netsh commands & the powershell command work and I'll post a complete version for you.

    Denis
    Last edited by Try3; 22 Oct 2019 at 23:47.
      My Computer


  8. Posts : 12
    Windows 10 64
    Thread Starter
       #8

    Code:
    netsh interface set interface "Wi-Fi 2" disabled
    netsh interface set interface "Wi-Fi 2" enabled
    The above command works for setting and resetting the adapter though I always do with via UI when the problem manifests. After making this post the problem so far didn't occur so I used it when it was working. Don't know why my connection is Wi-Fi 2, even though I have just one and I can't rename it but anyway.

    And yes, the poweshell command you listed also works. I would appreciate if you could write the complete batch file for me.

    Thanks.
      My Computer


  9. Posts : 11,352
    Windows 10 Home x64 Version 21H2 Build 19044.1706
       #9

    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 suggest
    WshShell.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
    How to automate resetting the adapter? Attached Files
    Last edited by Try3; 24 Oct 2019 at 10:05.
      My Computer


  10. Posts : 12
    Windows 10 64
    Thread Starter
       #10

    Thanks @Try3. Just one question, how to I increase the time from 10sec to 1 minute or anything if in future I wanted to do that? Also, I need to run it as Admin, right?
      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

Designer Media Ltd
All times are GMT -5. The time now is 19:53.
Find Us




Windows 10 Forums