how to execute a batch when computer is on IDLE

Page 4 of 7 FirstFirst ... 23456 ... LastLast

  1. Posts : 1,211
    Windows 10
       #31

    batch file so cmd interpreted. But hey what would i know right
      My Computer


  2. Posts : 16,963
    Windows 10 Home x64 Version 22H2 Build 19045.4170
       #32

    m18xr2 said:
    these commands are powershell? or cmd?
    It's a batch file, so it's all in cmd.


    Denis
      My Computer


  3. Posts : 181
    10, server 2016, server2012
    Thread Starter
       #33

    Try3 said:
    It's a batch file, so it's all in cmd.


    Denis
    ok thanks i'll give them a try!
      My Computer


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

    If you'd like to post your existing batch file then I could integrate that into my first one as well to give you a comparison to look at.


    Denis
      My Computer


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

    Try3 said:
    If you'd like to post your existing batch file then I could integrate that into my first one as well to give you a comparison to look at.


    Denis
    yea I was having trouble getting it going but I dont think its the code. I think windows PS is lacking some libraries or some kind cause each time I execute command that works for other, it just says it doesn't recognize it.

    but my batch is simple, its just a line in .PS1 file.

    Restart-NetAdapter -Name "Wi-Fi 13"
      My Computer


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

    Actually, I thought yours was a batch script not a PowerShell script but here's how I can integrate it within my suggested framework [post #28 above].
    The script needs to be run by TS on Idle and as Admin and TS needs to run it only if a network connection is detected.
    Then the script will achieve what you want.
    You can call the script whatever you want as long as you leave its file extension as .bat
    I've left in my explanatory comments but you can remove them to simplify the picture if you want.

    I assume the WiFi adapter name you are using is correct [Wi-Fi 13] as shown in the Network & sharing centre dialogs
    Code:
    ncpa.cpl


    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


    If you have only a single WiFi adapter then the script could be improved by detecting the WiFi adapter's name then using it. This will be handy if you continue to experiment with drivers and any of them cause its name to change.



    Best of luck,
    Denis
      My Computer


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

    Try3 said:
    Actually, I thought yours was a batch script not a PowerShell script but here's how I can integrate it within my suggested framework [post #28 above].
    The script needs to be run by TS on Idle and as Admin and TS needs to run it only if a network connection is detected.
    Then the script will achieve what you want.
    You can call the script whatever you want as long as you leave its file extension as .bat
    I've left in my explanatory comments but you can remove them to simplify the picture if you want.

    I assume the WiFi adapter name you are using is correct [Wi-Fi 13] as shown in the Network & sharing centre dialogs
    Code:
    ncpa.cpl


    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


    If you have only a single WiFi adapter then the script could be improved by detecting the WiFi adapter's name then using it. This will be handy if you continue to experiment with drivers and any of them cause its name to change.



    Best of luck,
    Denis
    forgot to ask, do I need to use this script injunction with task schedular? otherwise how would computer detect idling and run this on its own?

    edit: I just re-read it, TS = task schedular. what about ncpa.cpl what is that?
      My Computer


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

    m18xr2 said:
    what about ncpa.cpl what is that?
    That command takes you to the Network & sharing centre, Adapter settings dialog so you can check the adapter name you are using [Wi-Fi 13] is correct.
    Right-click on the Start button, select Run and put that command in there.
    how to execute a batch when computer is on IDLE-nasc-adapters.png
    So, for example, I would use WiFi 2.


    All the best,
    Denis
      My Computer


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

    Try3 said:
    That command takes you to the Network & sharing centre, Adapter settings dialog so you can check the adapter name you are using [Wi-Fi 13] is correct.
    Right-click on the Start button, select Run and put that command in there.
    how to execute a batch when computer is on IDLE-nasc-adapters.png
    So, for example, I would use WiFi 2.


    All the best,
    Denis
    question, about %OutputFolder%\%OutputFile% do I manually enter an output directory myself?

    also, if I double click the PS file, brief window comes and exit and I have no idea if it works or not. but when I try to run the powershell script in PS window via cli, such as newwifires.ps1 (name of the file I saved it as) it won't run. it just show the below:

    how to execute a batch when computer is on IDLE-ps1.png
      My Computer


  10. Posts : 16,963
    Windows 10 Home x64 Version 22H2 Build 19045.4170
       #40

    %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
      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:44.
Find Us




Windows 10 Forums