Task scheduler cannot open batch file when set to run whether user is logged on or not

97,313

Solution 1

I have resolved this problem with help from a contractor who was doing some other work for my institution.

In the properties window for the task, on the Action tab, when creating or editing the action that opens the batch file (in my case test.bat), there is the "Program/script:" field and there is also a "Start in (optional):" field. I previously had the "Program/script:" field containing the full path to the file, ending with the file name, and I had the "Start in (optional):" field null. This configuration appears to work without issue when the task is set to "Run only when user is logged on" in the General tab of the task's properties window. However, this configuration does not work when the task is set to "Run whether user is logged on or not".

In order to resolve the problem, I changed the action so that the "Program/script:" field contained only the file name, not the file path. I put the file path in the "Start in (optional):" field. This configuration works when the task is set to "Run only when user is logged on"! I have tested with the task running on a trigger and also with running it manually.

Solution 2

Not sure if this helps. My issue was using a interactive program (in particular Excel) as well.

Creating the 2 empty folders (Desktop) solved it. Create both even if you are running on x64.

C:\Windows\System32\config\systemprofile\Desktop
C:\Windows\SysWOW64\config\systemprofile\Desktop

I debugged my code and realised the code exited at this line

ExcelFile excelFile = new ExcelFile(directory, filename);

I read this solution somewhere and I cannot find the URL now.

Solution 3

Try this as well..

Resolved the "Run whether user is logged on or not" by setting the “Log on as Batch Job” security policy.

Here is details on the setting… https://danblee.com/log-on-as-batch-job-rights-for-task-scheduler/

You may take another approach, but here is the steps… 1) I updated the default domain group policy by adding my admin account (that I fire the task with) to the “Log on as Batch Job” security policy. (Even though ADMIN group is listed, and I am using an admin account) 2) I forced GPUPDATE on the client 3) Rebooted the client 4) Looked at the policy on the client to make sure it made it over from the DC 5) Set the task "Run whether user is logged on or not" 6) Ran the task on demand and it worked creating a PDF file. 7) Logged out and ran the task at scheduled time and it worked creating a PDF file.

Solution 4

What work for me was make sure that the path is available.

i used net use Z: \computername\folder /persistent:yes

then the xxcopy /s /c /d /e /i /y D:*.* z:\BackupBat

I notice all my other .bat work fine only the one with the reference to a map folder was the one no running, so that need to be the problem. with these change the bat file runs good.

Share:
97,313

Related videos on Youtube

MasterOfNone
Author by

MasterOfNone

I was hired as a database administrator at a seminary in July 2013. I am am MCP in Querying SQL Server 2012 and one exam away from my MCSA.

Updated on March 22, 2020

Comments

  • MasterOfNone
    MasterOfNone about 4 years

    OS: Windows Server 2008 R2 Enterprise

    I am unable to get the Task Scheduler to run .bat files while I am logged off. I have a a production .bat file I want to use, but for my troubleshooting purposes I am using C:\Users\Administrator\Desktop\test.bat

    test.bat is very simple. All it contains is: taskkill /im notepad.exe

    It runs successfully on its own. If I have Notepad open and I double-click test.bat then the cmd window flashes and Notepad dies. The .bat file is not the problem.

    The task I created in Task Scheduler is "Test taskkill" (without the quotes). I can see its location in Windows Explorer: C:\Windows\System32\Tasks\Test taskkill

    If the task is set to run only when the user is logged on then the task works. With this setting, I can right-click on it > run and I get the same behavior as if I double-click the .bat file. That means the task is configured correctly.

    If I set it to run whether or not the user is logged on then when I right-click > run nothing obvious happens. The job is reported as having been run successfully in the history, but Notepad survives. I know that I am using the proper credentials for the account that I am configuring to run the task. That account is the local administrator.

    This issue appears similar to issues other have had in the past:

    Running a .bat file in Scheduled Task

    http://social.technet.microsoft.com/Forums/windowsserver/en-US/d47d116e-10b9-44f0-9a30-7406c86c2fbe/scheduled-task-wont-run-bat-file?forum=winservermanager

    The thread in the second link seemed to resolve an identical issue for many people with the suggestion that the account used to run the task requires explicit permission to the .bat file and all files that the .bat file modifies. This was very promising (if an annoying requirement). However, I have confirmed or assigned explicit permissions for the local administrator to the following areas, and I am still not successful:

    • C:\Users\Administrator\Desktop\
    • C:\Users\Administrator\Desktop\test.bat
    • C:\Windows\System32\Tasks\
    • C:\Windows\System32\Tasks\Test taskkill

    Additionally, I have confirmed that the local administrator account is in the local administrators group.

    Am I missing some other permissions that needs to be set? Is there something else I should be looking at? Thanks!

    • ths
      ths over 9 years
      did you check the "run with highest privileges" checkbox?
    • MasterOfNone
      MasterOfNone over 9 years
      Sorry, yes. I should have specified that, but I did indeed do so.
    • MasterOfNone
      MasterOfNone over 9 years
      I may have resolved this. I will let you know soon (probably tomorrow).
    • TharsDimitris
      TharsDimitris about 5 years
      I have exactly the same issue in windows 10
  • TylerH
    TylerH about 5 years
    So your issue was that you had a typo? That is probably not very useful for others.
  • FerFig
    FerFig about 5 years
    No, it was not a typo! The space was really in the path because the path of the batch file had a space. What I did was changed the path of the batch removing the space; i.e. it was on "D:\My Jobs\executethis.bat" to "D:\MyJobs\executethis.bat" I needed to create a new directory without the space.
  • Adir D
    Adir D over 3 years
    doesn't work for me, only if i change the selection of radio button. maybe you can upload some screenshots how exactly you populated the fields.
  • David
    David almost 3 years
    This one worked for me when using excel macros in the bat file.
  • AquaAlex
    AquaAlex about 2 years
    every time this is the issue on my servers