how to execute a batch when computer is on IDLE

Page 6 of 7 FirstFirst ... 4567 LastLast

  1. Posts : 16,950
    Windows 10 Home x64 Version 22H2 Build 19045.4170
       #51

    So my suggested revised batch file is this.
    It's simpler than I thought because there is only a need to have a single looping control variable not two.
    I've remarked out the Pause line at the end for you.
    You'll still want to item it to check that it is completing within the 1 minute TS interval.

    Code:
    :: Initialisation
    Prompt $g
    Set WWWConnection=Off
    Set "OutputFolder=%Temp%"
    Set "OutputFile=%Random%-WWWConnection-TestResults"
    Set /a TestCounter=0
    
    :DoTheTest
    :: Run test every 10 secs to use up the 1 minute between TS starting this task again.
    TimeOut /T 10
    Set /a TestCounter=%TestCounter%+1
    powershell -Command "Invoke-WebRequest http://www.msftconnecttest.com/connecttest.txt -OutFile '%OutputFolder%\%OutputFile%'"
    dir "%OutputFolder%\%OutputFile%"
    If %ErrorLevel% GTR 0 GoTo RestartAdapter
    If %ErrorLevel% EQU 0 GoTo RepeatTheTest
    
    :: Restart the WiFi network adapter if the internet network connection exists but does not function i.e. no web addresses are working
    :RestartAdapter
    ::::: This is your existing PS code for resetting your network adapter
    powershell.exe restart-netadapter -Name 'Wi-Fi 13'
    
    :RepeatTheTest
    ::  Repeat the test if less than a minute has passed i.e. TestCounter < 6
    del "%OutputFolder%\%OutputFile%"
    If TestCounter LSS 6 GoTo DoTheTest
    
    :: EndWWWConnectionTest
    :: Pause at the end during testing - WWWConnection is %WWWConnection%
    :: The batch file now closes of its own accord
    
    :: 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.  
    :: If you were to run this non-minimised, you would see some red text as it failed to find the www.msftconnecttest.com/connecttest.txt file


    Denis
      My Computer


  2. Posts : 181
    10, server 2016, server2012
    Thread Starter
       #52

    Try3 said:
    So my suggested revised batch file is this.
    It's simpler than I thought because there is only a need to have a single looping control variable not two.
    I've remarked out the Pause line at the end for you.
    You'll still want to item it to check that it is completing within the 1 minute TS interval.

    Code:
    :: Initialisation
    Prompt $g
    Set WWWConnection=Off
    Set "OutputFolder=%Temp%"
    Set "OutputFile=%Random%-WWWConnection-TestResults"
    Set /a TestCounter=0
    
    :DoTheTest
    :: Run test every 10 secs to use up the 1 minute between TS starting this task again.
    TimeOut /T 10
    Set /a TestCounter=%TestCounter%+1
    powershell -Command "Invoke-WebRequest http://www.msftconnecttest.com/connecttest.txt -OutFile '%OutputFolder%\%OutputFile%'"
    dir "%OutputFolder%\%OutputFile%"
    If %ErrorLevel% GTR 0 GoTo RestartAdapter
    If %ErrorLevel% EQU 0 GoTo RepeatTheTest
    
    :: Restart the WiFi network adapter if the internet network connection exists but does not function i.e. no web addresses are working
    :RestartAdapter
    ::::: This is your existing PS code for resetting your network adapter
    powershell.exe restart-netadapter -Name 'Wi-Fi 13'
    
    :RepeatTheTest
    ::  Repeat the test if less than a minute has passed i.e. TestCounter < 6
    del "%OutputFolder%\%OutputFile%"
    If TestCounter LSS 6 GoTo DoTheTest
    
    :: EndWWWConnectionTest
    :: Pause at the end during testing - WWWConnection is %WWWConnection%
    :: The batch file now closes of its own accord
    
    :: 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.  
    :: If you were to run this non-minimised, you would see some red text as it failed to find the www.msftconnecttest.com/connecttest.txt file

    Denis
    thanks a lot Dennis, I will give it a try. I am thinking that if loop can be done via the batch then maybe TS could be removed entirely (with exception of course at the very start of logon to run this batch script, since the loop will be done by the batch itself).

    basically have this batch always run as a running process in the background, similar to an exe? or would that not work because its .bat? I really have no experience in this area at all

    edit: so I tried a couple of thing with the new script, theres some issue. whether the adapter is plugged in or not, the script only loops once and doesnt go to 6th time. so this means that even with TS restarting task every 1 min, if it happens to drop connection then right after TS task run and then drops again like 7-8 seconds later (which has happened before), it would not run for the rest of that min for like 50 seconds.

    edit 2: I tried by adding :top at top of your first script, as well as timer line into your first script and then at the bottom added goto :top and it loops continuously which is good. but the problem now becomes after it reset adapter just once, entire script stops running. otherwise it'll continue to run which is what I need
    Last edited by m18xr2; 07 Dec 2023 at 21:23.
      My Computer


  3. Posts : 16,950
    Windows 10 Home x64 Version 22H2 Build 19045.4170
       #53

    TS cannot be left out of it because it is TS that decides to only run the script if Windows thinks you are connected to a network - that's the condition you set on the task earlier on and it is essential.
    how to execute a batch when computer is on IDLE-ts-conditions.png


    Denis
      My Computer


  4. Posts : 181
    10, server 2016, server2012
    Thread Starter
       #54

    Try3 said:
    TS cannot be left out of it because it is TS that decides to only run the script if Windows thinks you are connected to a network - that's the condition you set on the task earlier on and it is essential.
    how to execute a batch when computer is on IDLE-ts-conditions.png


    Denis
    actually my TS didn't use that option, its just simply running it every min. also I tested without using TS today when connection was down, ran your script and it reseted my adapter just fine. Which made sense because your script basically checks if theres connection, I can't see why TS is needed

    is there a way to change the script so that it always loop, but once reset it doesn't auto close?

    cause right now it does just that after I added in the loop code. if I remove the adapter OR not connect to any wifi network, it'll loop endlessly trying to find it to reset it, but if theres currently a connection, it'll stop and simply say theres internet and the entire CMD window goes away and it doesn't loop.
      My Computer


  5. Posts : 16,950
    Windows 10 Home x64 Version 22H2 Build 19045.4170
       #55

    If you are running it all the time without that TS Condition then the script will reset your adapters if you have deliberately disconnected from your networks.
    It is TS that is checking that Windows believes you are connected to a network.

    The script is not intended to loop only once. I cannot see what would cause that.
    Oh ********** [three pages of foul language removed by forum moderators]
    Corrected version
    I've added a reset of the TestCounter at the end just for tidiness [but I cannot see any problems arising from not resetting it].
    I've also removed all the comments for no particular reason.
    Code:
    Prompt $g
    Set "OutputFolder=%Temp%"
    Set "OutputFile=%Random%-WWWConnection-TestResults"
    Set /a TestCounter=0
    
    :DoTheTest
    TimeOut /T 10
    Set /a TestCounter=%TestCounter%+1
    powershell -Command "Invoke-WebRequest http://www.msftconnecttest.com/connecttest.txt -OutFile '%OutputFolder%\%OutputFile%'"
    dir "%OutputFolder%\%OutputFile%"
    If %ErrorLevel% GTR 0 GoTo RestartAdapter
    If %ErrorLevel% EQU 0 GoTo RepeatTheTest
    
    :RestartAdapter
    powershell.exe restart-netadapter -Name 'Wi-Fi 13'
    
    :RepeatTheTest
    del "%OutputFolder%\%OutputFile%"
    If %TestCounter% LSS 6 GoTo DoTheTest
    Set /a TestCounter=0

    Denis
    Last edited by Try3; 08 Dec 2023 at 06:03.
      My Computer


  6. Posts : 181
    10, server 2016, server2012
    Thread Starter
       #56

    Try3 said:
    Denis
    heh the comment does help me understand how the code works and what its trying to do

    thanks a lot for the help for this. this one continue to try reset but stops at textcounter = 5+1, after hitting LSS 6 it just closes the CMD which I guess its fine I can just have this batch close and reopen by TS once a day or something, and set LSS to something like over 1 week in length.

    now I just gotta find a way to have this run in the background and make sure it works when wifi is really not connecting to the web.
      My Computer


  7. Posts : 16,950
    Windows 10 Home x64 Version 22H2 Build 19045.4170
       #57

    m18xr2 said:
    I can just have this batch close and reopen by TS once a day or something, and set LSS to something like over 1 week in length.
    Yes, you can juggle the TS frequency & the number of loops within the batch file.
    TS does not close the batch file. It can only run it.


    m18xr2 said:
    now I just gotta find a way to have this run in the background
    That's what I explained earlier.
    Try3 said:
    If this was my batch file, I would go a stage further after a week or so when I'd got used to the thing and it was working well.
    Make Task scheduler run a batch file minimised and with a specific icon - TenForums
    I cannot remember if I've used that procedure with batch files running as Admin.
    Whilst I normally choose to run scripts minimised, the article includes instructions on making them run hidden instead.


    m18xr2 said:
    make sure it works when wifi is really not connecting to the web
    That's what the TS Condition should achieve for you.


    Denis
      My Computer


  8. Posts : 181
    10, server 2016, server2012
    Thread Starter
       #58

    Try3 said:
    Yes, you can juggle the TS frequency & the number of loops within the batch file.
    TS does not close the batch file. It can only run it.



    That's what I explained earlier.

    Whilst I normally choose to run scripts minimised, the article includes instructions on making them run hidden instead.



    That's what the TS Condition should achieve for you.


    Denis
    didnt have to wait long. connection dropped today and reconnected and I only know of it due to gaming screen showing different outside of alt tab, also confirmed with adapter up-time got resetted.

    ty so much man. though I would still need to test to see if TS run just 1 instance of the batch and if it'll somehow recognize it, since theres no process name attach to it like a program.exe. task manager just show cmd.exe so if I have got multiple looping ones I would have no way to know which to kill and which to start, furthermore taskkill command would kill all looping batch too which is a shame.
      My Computer


  9. Posts : 16,950
    Windows 10 Home x64 Version 22H2 Build 19045.4170
       #59

    In Task mgr Processes tab, right-click on any column header and add Command line.
    how to execute a batch when computer is on IDLE-tmgr-processes-adding-column.png

    I've used TaskList to identify specific scripts in the past but I have no idea why you are talking about killing it.
    I can't remember if I went further and killed a specific script. My notes only show me finding out if specific scripts are running.
    The key to identifying them is to add a Title to the script because that sets the window name and that is a parameter than can be searched for.
    e.g. add the line
    Title Fred

    Here are the results of a demo run.
    how to execute a batch when computer is on IDLE-demo.png
    1,2 Test [batch] file sets its title to Fred and that becomes its window name when it is run.
    3,4 Detection script is called up using a shortcut to which BAT Fred has been added [the script works for BAT & PS1 scripts]
    5 The Fred window is identified.

    But this is all getting pretty complicated.
    Set TS to run your task every minute & set the TestCounter loops to something that can be relied upon to complete within a minute [6 was just a starting guess, I expect the result to be 4].


    Denis
    Last edited by Try3; 09 Dec 2023 at 05:59.
      My Computer


  10. Posts : 16,950
    Windows 10 Home x64 Version 22H2 Build 19045.4170
       #60

    Actually, I have used a similar TaskKill command. This is for a window called Fred.
    Code:
    taskkill /fi "WINDOWTITLE eq Fred"
    You do not need to add any more quotes if the window title has spaces. The example I found was for a window titled Keep display on
    Code:
    taskkill /fi "WINDOWTITLE eq Keep display on"


    Denis
      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 11:08.
Find Us




Windows 10 Forums