Windows Task Schedule does not run task but task runs successfully when batch file manually called or when script run in Visual Studio Code

glassesdumpbreathe 20 Reputation points
2024-12-02T20:02:15.0866667+00:00

Summary: My .bat file runs when I manually double-click the file in File Explorer, but it does not run when I try to schedule it in Windows Task Scheduler.

Details:

I have a .bat file that I have stored in a folder. When I open File Explorer and double-click that .bat file, it successfully runs. The .bat file runs a Python script. I am also able to successfully run the Python script manually from Visual Studio Code.

However, when I schedule that same file in Windows Task Scheduler, WTS tells me that it completed the task successfully but when I check my logs I find that the task did not actually run.

When I last triggered the bat file to run in Windows Task Scheduler at 1:11:41 PM, it started the task (100), started the action (200), completed the action (201) and completed the task (102) at 1:11:42 PM.

This is the XML view for the task completed (102) event:

  <Provider Name="Microsoft-Windows-TaskScheduler" Guid="{de7b24ea-73c8-4a09-985d-5bdadcfa9017}" />

  <EventID>102</EventID>

  <Version>0</Version>

  <Level>4</Level>

  <Task>102</Task>

  <Opcode>2</Opcode>

  <Keywords>0x8000000000000001</Keywords>

  <TimeCreated SystemTime="2024-12-02T19:11:42.2075975Z" />

  <EventRecordID>525946</EventRecordID>

  <Correlation ActivityID="{4613b91d-77ba-455d-aa2d-fc89b76b6cc0}" />

  <Execution ProcessID="2800" ThreadID="42828" />

  <Channel>Microsoft-Windows-TaskScheduler/Operational</Channel>

  <Computer>(removed by OP)</Computer>

  <Security UserID="(removed by OP)" />

  </System>

  • <EventData Name="TaskSuccessEvent">

  <Data Name="TaskName">\pathfolder\file_name_without_file_extension</Data>

  <Data Name="UserContext">org_name\user_name</Data>

  <Data Name="InstanceId">{4613b91d-77ba-455d-aa2d-fc89b76b6cc0}</Data>

  </EventData>

  </Event>

I'm seeing in other Microsoft answers that if it can be problematic to supply a path to the Program/Script bank that begins with a letter for a drive. However, I'm not seeing specific information on what I should use instead of this letter drive name. During my googling, I saw another Microsoft Answers where someone linked to two previous Microsoft Answers, but the links were broken:

editaction

Here are screenshots of my Windows Task Scheduler with sensitive information removed:

General tab:

bat general

Triggers:

bat trigger

Actions:

bat actions

Edit Action--please note I have tried placing this path within quotation marks and without quotation marks (in screenshot below):

editaction

Conditions:

bat conditions

Settings:

bat settings

This is information on my computer:

Processor 13th Gen Intel(R) Core(TM) i9-13900H 2.60 GHz

Installed RAM 64.0 GB (63.7 GB usable)

System type 64-bit operating system, x64-based processor

Edition Windows 11 Enterprise

Version 22H2

Windows 11
Windows 11
A Microsoft operating system designed for productivity, creativity, and ease of use.
10,733 questions
0 comments No comments
{count} votes

Accepted answer
  1. MotoX80 35,506 Reputation points
    2024-12-02T23:14:16.74+00:00

    The task scheduler does a user impersonation, not a full desktop logon. Your H drive is probably not getting mapped.

    Change the "Program/script" to execute cmd.exe. In the "Add arguments (optional)" set it to "/c H:\whatever\whatever.bat 1> c:\YourFavoriteLogFolder\YourLog.txt 2>&1"

    That will cause cmd.exe to capture stdout and stderr into a log file that you can review.

    If it does not see the H drive, then use the UNC path to the bat file, "\\YourServerName\YourShareName\YourFolderName\whatever.bat."

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.