batch file from scheduled task returns code 2147942401
Asked Answered
M

10

40

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

Millenary answered 19/1, 2018 at 15:5 Comment(5)
\\server\sharename\folder\file.ext You only have \server\...Galeiform
Make sure the task is run with credentials that allow access to the network share: learn.microsoft.com/en-us/previous-versions/windows/it-pro/…Misalliance
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 especially because StackOverflow is for seeking help with your failing code, without your code the question is technically off-topic!Ashbaugh
For me, "start in" was empty and causing this issue. Setting this value resolved the issue.Godroon
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)Hysterotomy
S
35

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.

Sadye answered 14/5, 2018 at 22:54 Comment(5)
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.Forsook
So then, does it resolve to SCHED_S_TASK_RUNNING as per msdn?Sadye
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.Forsook
@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.Ridicule
@johnblair The comment of Christian is correct, but ambiguous. The code is an COM error code as described here: learn.microsoft.com/en-us/windows/win32/com/… So, 0x80070000 + an Windows error code from GetLastError() is the base for this error. Error code 1 ist ERROR_INVALID_FUNCTION: learn.microsoft.com/en-us/windows/win32/debug/… Thus, this code has nothing to do with the exit code of the process.Fist
L
19

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.

Luehrmann answered 24/7, 2018 at 13:29 Comment(0)
P
11

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.

Pisolite answered 10/7, 2019 at 13:16 Comment(3)
Could also be because there is a blank in the path to the ps1 file. #45760957Scleroma
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 ...Kurzawa
Thank you for this. This one is easy to miss!Cerate
S
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"
Serf answered 13/9, 2019 at 17:51 Comment(0)
A
5

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.

Autonomic answered 16/12, 2020 at 16:8 Comment(0)
T
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.

Tenuous answered 14/3, 2019 at 12:33 Comment(0)
G
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
Gershon answered 21/4, 2022 at 17:32 Comment(0)
G
0

Just make sure for each action (.bat files in my case) that the Start-in (optional) path is set to the same folder as the file and does not end with a backslash.

Peter

Gainer answered 7/6, 2024 at 13:4 Comment(0)
S
0

I had the same error code in the task scheduler.

The reason was that the user account was set to my user instead of the SYSTEM user. This was important, because the task should run on system start without any user login.

The user account can be set in the "General" tab under "Security settings".

Succuss answered 1/7, 2024 at 8:26 Comment(0)
S
0

I had the same error from Task Scheduler but it happened to me when my ps script had incorrect digital signature. I was using ExecutionPolicy with 'AllSigned' and my ps script was digitally signed but meantime I changed something inside the script and the script has to be signed again after such action.

I found out when I ran the script manually from ps and I got this error: {script.ps1} is not digitally signed. You cannot run this script on the current system.

Schaaff answered 19/7, 2024 at 9:53 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.