windows 来自计划任务的批处理文件返回代码 2147942401

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48343993/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 20:02:02  来源:igfitidea点击:

batch file from scheduled task returns code 2147942401

windowsbatch-filescheduled-tasks

提问by Sbanga

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

我正在尝试安排作业以使用 Windows 10 任务计划程序运行批处理文件,但它导致返回代码 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"

批处理文件位于远程位置,因此我提供绝对路径
“\\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 - GeneralActions - Edit Action

如果我使用命令提示符运行批处理脚本,那么它工作正常。 属性 - 一般动作 - 编辑动作

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

批处理脚本是将excel转文件转成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

它调用另一个 VB 脚本,它是 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

如果我按照@Steinspecht 所述在“操作”选项卡中进行更改(任务调度程序-任务“成功”完成,退出代码为 0x8007001),那么我也会收到代码 2147942401 不确定添加参数是否写入正确在此处输入图片说明

回答by woodvi

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 superuserdescribes 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".

Task Scheduler 的错误代码在msdn中列为十六进制,您的代码 2147942401 转换为十六进制为 0x80070001(此处未列出),但该超级用户将其描述为“非法功能”。他通过使用“最简单的任务调度程序设置,现在它可以工作”解决了他的问题。我注意到他只在用户登录时运行他的任务,所以他不需要“作为批处理作业登录”。

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

Social Technet,您可以分配该特权

  • 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.
  • 输入secpol.msc /s
  • 在MSC管理单元中选择“本地策略
  • 选择“用户权限分配
  • 右键单击“作为批处理作业登录”并选择属性
  • 单击“添加用户或组”,并包含相关用户。

Local Security Policy Snap-In

本地安全策略管理单元

Your task calls a network resource. These powershell scriptersrecommend bringing those resources to your local machine to eliminate any chance of network/connectivity/permissions issues.

您的任务调用网络资源。这些powershell 脚本编写者建议将这些资源带到您的本地计算机,以消除任何网络/连接/权限问题的可能性。

回答by M Herbener

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

即使所有任务调度程序设置、权限等都正确,此错误代码也可能由实际 Powershell 脚本中的错误/错误引起;就我而言,我引用了一个不存在的目录。

回答by user3507000

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

我知道一个老问题,但我在 Windows 2016 服务器上收到 2147942401 错误。

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.

如果查看计划任务属性,在“常规”选项卡底部,默认为“配置”:Windows Vista、Windows Server 2008。

Changed to Windows Server 2016 and the problem was solved.

改为Windows Server 2016,问题解决。

回答by BrianCanFixIT

Throwing another common cause of the error action "powershell.exe" with return code 2147942401here. 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.

"powershell.exe" with return code 2147942401在这里抛出错误操作的另一个常见原因。如果您的操作参数不正确,您也会收到此错误消息。检查操作参数参数和参数值的间距是否正确。

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"

回答by paulH

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:

M Herbener 的回答导致我尝试手动运行脚本,以查看脚本是否有错误。它没有,但是当我收到错误消息时,它确实突出了问题所在:

[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.

当然,解决方案是运行 Set-ExecutionPolicy 以允许 Powershell 脚本运行。