'batch file from scheduled task returns code 2147942401

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



Solution 1:[1]

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.

Solution 2:[2]

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.

Solution 3:[3]

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.

Solution 4:[4]

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"

Solution 5:[5]

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.

Solution 6:[6]

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.

Solution 7:[7]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 daveloyall
Solution 3 user3507000
Solution 4 BrianCanFixIT
Solution 5 paulH
Solution 6
Solution 7 Omer Celik