how to execute a batch when computer is on IDLE

Page 5 of 7 FirstFirst ... 34567 LastLast

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

    Oh and I meant to add -

    If you ever get inexplicable behaviour in your computer, try logging in to another user account to check if it happens there as well.
    I always have a pair of spare, Admin, password-protected local user accounts for use in case of suspected user profile corruption.

    I can see that you are logged in to the Built-in Admin account.
    I have no reason to think that affects your current efforts but the behaviour of that account is undocumented and has been known to have changed over the years [it can now run Apps which it could not do a few years ago].
    Using the Built-in Admin account does not give you any advantages, by the way.


    All the best,
    Denis
      My Computer


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

    Try3 said:
    %OutputFolder%\%OutputFile%
    Those variables are set within the script - see lines 4 & 5.

    There is no PS1 file to run. It's a batch script. "You can call the script whatever you want as long as you leave its file extension as .bat"
    It calls a powershell command on one line but it remains a batch script.
    You can run it manually but, as with running it through TS, it must be run as Admin.

    Denis
    heh im trying to figure this out. the batch did work minus 1 portion, somehow its trying to run PS for this section and PS not recognizing the command? screenshot below

    how to execute a batch when computer is on IDLE-batch-result.png

    though however it did try 3 times like your script mentioned but it ended up reset the wifi adapter in the end, even though the wifi is working. shouldnt it not reset if its working? or was it due to the command above not recognized hence the files not created, thus it reset adapter thinking it doesn't have internet based on missing file.
      My Computer


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

    Your output folder path contains spaces. That's what is creating that response. I didn't write the script to cater for that.
    If you change your output folder to one that contains no spaces [or special characters such as &] then it will run.

    And, yes, the wifi reset is being run because the code is failing so it thinks it has no connectivity.


    Denis
      My Computer


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

    I've worked out how to cope with a space or special character.

    Code:
    :: Initialisation
    Prompt $g
    Set WWWConnection=Off
    Set "OutputFolder=%Temp%"
    Set "OutputFile=%Random%-WWWConnection-TestResults"
    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%\%OutputFile%'"
    dir "%OutputFolder%\%OutputFile%"
    If %ErrorLevel% GTR 0 (Set WWWConnection=Off) & (If %TestCounter% LSS 3 GoTo DoTheTest)
    If %ErrorLevel% EQU 0 (Set WWWConnection=On) & (del "%OutputFolder%\%OutputFile%")
    
    ::Detect the result
    If "%WWWConnection%"=="On" GoTo EndWWWConnectionTest
    
    :: Restart the WiFi network adapter if the internet network connection exists but does not function i.e. no web addresses are working
    ::::: This is your existing PS code for resetting your network adapter
    powershell.exe restart-netadapter -Name 'Wi-Fi 13'
    
    :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

    I've only tested it a few times.
    The OutputFolder path must exist already but that's never been a problem for me since I've always just set it to %Temp%


    Denis
      My Computer


  5. Posts : 181
    10, server 2016, server2012
    Thread Starter
       #45

    Try3 said:
    I've worked out how to cope with a space or special character.

    Code:
    :: Initialisation
    Prompt $g
    Set WWWConnection=Off
    Set "OutputFolder=%Temp%"
    Set "OutputFile=%Random%-WWWConnection-TestResults"
    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%\%OutputFile%'"
    dir "%OutputFolder%\%OutputFile%"
    If %ErrorLevel% GTR 0 (Set WWWConnection=Off) & (If %TestCounter% LSS 3 GoTo DoTheTest)
    If %ErrorLevel% EQU 0 (Set WWWConnection=On) & (del "%OutputFolder%\%OutputFile%")
    
    ::Detect the result
    If "%WWWConnection%"=="On" GoTo EndWWWConnectionTest
    
    :: Restart the WiFi network adapter if the internet network connection exists but does not function i.e. no web addresses are working
    ::::: This is your existing PS code for resetting your network adapter
    powershell.exe restart-netadapter -Name 'Wi-Fi 13'
    
    :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
    I've only tested it a few times.
    The OutputFolder path must exist already but that's never been a problem for me since I've always just set it to %Temp%

    Denis
    lol yep I googled the issue it was spacing in between the links/folder name. I tried to put quotation on the "%temp" but clearly failed. your new one had quotation including entire output line minus the command set and that worked.

    now how to run this silent? when I manually run this in CMD via cli, it just stuck at the last line "press any key to continue" and doesn't close itself, also pressing a key does nothing and also doesn't close itself.
      My Computer


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

    When you've finished testing it, just add a pair of colons before the last line to make it into a remark rather than a command.
    Code:
    :: Pause at the end during testing - WWWConnection is %WWWConnection%
    But it should have closed itself when you pressed a key anyway.

    Just for info, I made it work [despite spaces or &] by
    - Changing the Set lines. Enclosing both the variable name and its value in quotation marks is a useful way of setting complex variable values.
    - Changing the powershell command line. The whole of the command was enclosed in quotation marks already. I added apostrophes around the '%OutputFolder%\%OutputFile%' part to make powershell accept the whole thing [it's sort of an equivalent to using quotation marks in batch file commands].

    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.

    But it does occur to me that I'm being a bad influence by suggesting further refinements once the thing is working.
    Once you're armed with this workaround, it would be better for you to return to the subject of the network adapter problem itself.


    Denis
      My Computer


  7. Posts : 181
    10, server 2016, server2012
    Thread Starter
       #47

    Try3 said:
    When you've finished testing it, just add a pair of colons before the last line to make it into a remark rather than a command.
    Code:
    :: Pause at the end during testing - WWWConnection is %WWWConnection%
    But it should have closed itself when you pressed a key anyway.

    Just for info, I made it work [despite spaces or &] by
    - Changing the Set lines. Enclosing both the variable name and its value in quotation marks is a useful way of setting complex variable values.
    - Changing the powershell command line. The whole of the command was enclosed in quotation marks already. I added apostrophes around the '%OutputFolder%\%OutputFile%' part to make powershell accept the whole thing [it's sort of an equivalent to using quotation marks in batch file commands].

    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.

    But it does occur to me that I'm being a bad influence by suggesting further refinements once the thing is working.
    Once you're armed with this workaround, it would be better for you to return to the subject of the network adapter problem itself.


    Denis
    thanks a lot man. I am testing it as of this moment, first to see if TS still doing it on "idle" when its not really idling. then I can go steps further on silent/background mode. gonan run this for 2-3 days
      My Computer


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

    m18xr2 said:
    ... first to see if TS still doing it on "idle" when its not really idling ...
    I think TS will continue to initiate the task when it decides the computer is idling but this should not be a problem because
    - it will only run the batch file if Windows thinks you are connected to a network, and
    - the batch file will only reset the adapter if it discovers that you don't really have any network connectivity after all.


    Best of luck,
    Denis
      My Computer


  9. Posts : 181
    10, server 2016, server2012
    Thread Starter
       #49

    Try3 said:
    I think TS will continue to initiate the task when it decides the computer is idling but this should not be a problem because
    - it will only run the batch file if Windows thinks you are connected to a network, and
    - the batch file will only reset the adapter if it discovers that you don't really have any network connectivity after all.

    Best of luck,
    Denis
    hey Denis, few things after trying this for a day or 2.

    - TS seems to run this every 1 minute as the lowest number, can't go any lower like every 15 or 30 seconds, even changing the xml and import them doesn't seem to work, not sure if theres a way to run it more frequent since it doesn't reset unless theres no connection.

    - I changed the connection attempt from 3 to 2 as I feel taht resetting took a bit too long and in gaming session it needs to reconnect quicker, would there be an issue if its changed to 2 attempts?

    - at every 1 min run TS frequency (currently as lowest), window will pop up and steal focus and also not auto close until I press any key, so I had to run this batch file via another batch file which uses 'start "" /min' and with TS running the silent batch, it will no longer steal focus but window still won't auto close. after like 20 mins I got 20 unclosed cmd window LOL

    - to expand the above Ive been searching on stack exchange to no avail, I ve also tried running in TS "whether user is logged on or not" which is equivalent of silent/background run but the last run task doesn't seem to update at all, and only seem to be 1 day repeat

    not sure if there are ways to improve these points
      My Computer


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

    I don't think TS can repeat things more frequently than 1 minute.
    But you could alter the batch file instead.
    - In the Initialisation section, create a new variable LoopCounter
    - Put in a TimeOut of 10 seconds in the line below the PS cmd, then, on the next line,
    - Reset the TestCounter back to 0, then, on the next line,
    - Add 1 to the value of LoopCounter, then, on the next line,
    - Check that LoopCounter is not greater than 6 {so the batch file does not run over the 1 minute that TS is using}, then, on the same line,
    - Start testing again.
    But you'd need to time some runs. Given that the powershell cmd takes time, you might well find that the TimeOut needs to be less than the 10 seconds I have suggested.

    Code:
    Set /a LoopCounter=0
    ...
    ...
    ...
    ...
    TimeOut /t 10
    Set /a TestCounter=0
    Set /a LoopCounter=%LoopCounter% + 1
    If %LoopCounter% LSS 6 GoTo DoTheTest

    I don't see how changing the connection attempt from 3 to 2 helps you in the original batch file.
    But if the whole lot is to be looped to cover as much as possible of the TS 1 minute, the current looping is not achieving anything and can be removed from the revised batch file.

    I still do not understand why the batch file is not closing at the end.
    - I assume you remarked out the Pause at the end as we discussed earlier.
    - I assume you have not added anything.

    This will take care of the stealing focus issue -
    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'll be back tomorrow. I'm about to have my dinner.


    All the best,
    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 12:54.
Find Us




Windows 10 Forums