37

I am trying to schedule a job to run a batch file with Windows 10 Task Scheduler, but it results in return code 2147942401.

The batch file is on remote location so I am giving the absolute path
"\\server1\file transfers\data files\inbound\abc\csv\excel-to-csv.bat"

If I run the batch script with command prompt then it work fine. Properties - General Actions - Edit Action

The batch script is to convert excel to file to csv.

Content of the script is:

FOR /f "delims=" %%i IN ("\\server1\file transfers\Data Files\Inbound\abc\CSV\*.xlsx" ) DO to-csv.vbs  "\\server1\file transfers\Data Files\Inbound\abc\*.xlsx" "%%~ni.csv"

Its calling another VB script which is to-cvs.vbs

If i make changes in Action tab as mention by @Steinspecht(Task scheduler- task completed “successfully” with exit code 0x8007001) then also i am getting the code 2147942401 Not sure whether Add a arguments is written correctenter image description here

S B
  • 519
  • 1
  • 4
  • 10
  • 1
    `\\server\sharename\folder\file.ext` You only have `\server\...` – ACatInLove Jan 19 '18 at 15:12
  • 1
    Make sure the task is run with credentials that allow access to the network share: https://learn.microsoft.com/en-us/previous-versions/windows/it-pro/windows-server-2008-R2-and-2008/cc722152(v=ws.11) – Michael Burr Jan 19 '18 at 15:12
  • Showing just one screenshot image of part of the process, no further information about your scheduled task and no information or content of the batch file does not really constitute a properly formed question with sufficient content to attract responders. Please consider [editing your question](https://stackoverflow.com/posts/48343993/edit) especially because StackOverflow is for seeking help with your failing code, without your code the question is technically off-topic! – Compo Jan 19 '18 at 15:13
  • 5
    For me, "start in" was empty and causing this issue. Setting this value resolved the issue. – Caramiriel Apr 24 '19 at 07:33
  • I just discovered that PowerShell imposes additional restrictions on remote scripts. I had this same code because my script was on a network share. I had to run the script with `-ExecutionPolicy Bypass`. For some reason I would see an error when running the remote script from an interactive shell, but in the end it decided to tell me: `cannot be loaded because you opted not to run this software now`. It also suggests that I use the `Unblock-File` cmdlet, but that's not an option in my case (using GPOs to run the script at startup on domain machines) – Radu C Sep 24 '21 at 11:29

7 Answers7

33

The error codes for Task Scheduler are listed as hexadecimal at msdn, and your code 2147942401 converts to hex as 0x80070001 (which is not listed there), but this superuser describes it as an "Illegal Function". He fixed his problem by using "the simplest task scheduler settings and now it works". I note that he only runs his task when the user is logged in, so he doesn't need "Log on as a batch job".

If you want to run the batch job when you're not logged in, you need a special privilege called "Log on as a batch job". Note that there is also a "DENY log on as a batch job" privilege, which you wouldn't want.

From Social Technet, you can assign that privilege with

  • Type in secpol.msc /s
  • Select "Local Policies" in MSC snap in
  • Select "User Rights Assignment"
  • Right click on "Log on as batch job" and select Properties
  • Click "Add User or Group", and include the relevant user.

Local Security Policy Snap-In

Your task calls a network resource. These powershell scripters recommend bringing those resources to your local machine to eliminate any chance of network/connectivity/permissions issues ... but that may not always be appropriate or practical.

woodvi
  • 1,898
  • 21
  • 27
  • 2
    The reported error code 2147942401 is really Windows' way of saying "code 1". The other bits are metadata, according to my ops guy. It says 2147942499 when you return 99. – Christian Davén Jun 29 '21 at 05:59
  • So then, does it resolve to SCHED_S_TASK_RUNNING as per [msdn](https://learn.microsoft.com/en-us/windows/win32/taskschd/task-scheduler-error-and-success-constants)? – woodvi Aug 02 '21 at 16:24
  • No, `SCHED_S_TASK_RUNNING` is a task status, not the executable's return code. A task can have a "running" status, but the executable cannot return a code until it is finished. – Christian Davén Aug 17 '21 at 14:52
  • @ChristianDavén Is there a link to a page that describes error code 2147942401 is really Windows' way of saying "code 1? This would make sense for me as my batch file is using robocopy where its exit code of 1 is all files copied successfully. – john blair May 18 '23 at 19:18
18

This error code can also result from a bug/mistake in the actual Powershell script or Batch (.bat) file, even if all task scheduler settings, permissions, etc. are correct; in my case I was referencing a directory that doesn't exist.

daveloyall
  • 2,140
  • 21
  • 23
M Herbener
  • 584
  • 3
  • 18
8

An old question I know, but I was getting 2147942401 error on windows 2016 server.

If you look at the scheduled task properties, on the bottom of the General Tab, it was defaulted to Configure for: Windows Vista, Windows Server 2008.

Changed to Windows Server 2016 and the problem was solved.

user3507000
  • 81
  • 1
  • 2
  • Could also be because there is a blank in the path to the ps1 file. https://stackoverflow.com/questions/45760457/how-to-run-a-powershell-script-with-white-spaces-in-path-from-command-line – André Schild Mar 27 '20 at 09:47
  • You saved my day! After trying all possible solution, fixed, and workarounds, this one worked on a Microsoft Windows Server 2016. Which for some reason defaulted all newly created scheduled tasks to the oldest possible "Compatibility Mode". And if the scheduled task is a PS script that relies on features that were not present in the age Windows Server 2008 ... – leonidos79 Mar 10 '21 at 13:14
  • Thank you for this. This one is easy to miss! – Thusal Hettiarachchi Apr 27 '22 at 08:30
5

Throwing another common cause of the error action "powershell.exe" with return code 2147942401 here. If your action arguments are not correct you will also get this error message. Check that the action argument parameters and parameter values are spaced correctly.

Good example:

-executionpolicy bypass -file "C:\Scripts\ImportFiles.ps1"

Broken Example (no space between the 'file' parameter and it's value):

-executionpolicy bypass -file"C:\Scripts\ImportFiles.ps1"
BrianCanFixIT
  • 51
  • 1
  • 4
3

For me, the task would sometimes work and sometimes wouldn't. According to the Scheduled Task History, when failing, it would appear as if it's been running for about 40 seconds, doing nothing, and completing action "C:\windows\SYSTEM32\cmd.exe" with return code 2147942401.

  • In this case, there was no point messing with the Group Policy settings because sometimes it would work. But not everytime. Which means it's a timing problem, not a Policy problem.

  • Recreating, reconfiguring my task (as suggested in this SuperUser Q&A) did not fix the problem.

  • I did also consider butchering my batch file and getting rid of the standard output redirection, thus abandonning the logging capability (and becoming blind). Or simply running an actual "*.exe" process, instead of using a batch file at all. This could potentially have been a solution.

  • I also considered replacing the "At startup" scheduled task by a full-blown Service, but this would have been an expensive experiment for such a trivial problem.

Ultimately, I remembered that services can be delayed: "Automatic" vs. "Automatic (Delayed Start)". So I imitated this by added a delay on the scheduled task, in the Tasks Scheduler. For "At startup" scheduled tasks, it's the trigger that have individual properties of its own, and that's where the delay can be configured:

Image depicting solution for delaying an on-startup scheduled task

I believe my scheduled task was sometimes being started a few milliseconds too early and some OS service or functionality was not yet available or allowed. Simply adding a small delay on the trigger fixed the problem.

numdig
  • 101
  • 4
2

M Herbener's answer led to me attempting to run the script manually, to see if the script had an error. It did not, but it did highlight what the problem was as I received the error message:

[my script] cannot be loaded because running scripts is disabled on this system.

The solution, of course, was to run Set-ExecutionPolicy to allow Powershell scripts to run.

paulH
  • 1,102
  • 16
  • 43
0

For me, the issue was file was blocked as it was downloaded from Internet. I was seeing this in task scheduler history

Task Scheduler successfully completed task "task name" , 
instance "{id}" , action "Powershell.exe" with return code 2147942401.

To solve this:

  • Right click .ps1 file and open Properties
  • Click Unblock under Attributes section
Omer Celik
  • 65
  • 1
  • 7