如何从任务计划程序启动的 cmd 文件中获取代码 202?
How do I get code 202 from a cmd file launched by Task Scheduler?
环境: Windows Server 2008 R2
Microsoft Windows Task Scheduler 执行一个 DoThis.cmd 文件,该文件总是 return 事件 201(成功)中的 ERRORLEVEL,即使虽然失败了:
Task Scheduler 成功 完成了任务“\Why Cant This Task Fail”,
实例“{c8655567-5f1c-4eb2-a2e1-91d2cd0405be}”,操作"C:\Windows\SYSTEM32\cmd.exe" ,代码return 399.
我希望 return 如果 ERRORLEVEL 为非零,则事件 202(失败),如果 returned 为零,则事件 201(成功)。
我的批处理文件DoThis.cmd包含
RETURN 399
谢谢,
~肖恩
我认为默认情况下这是不可能的。 任务本身 成功完成,因为它 运行 在预定的时间启动了您想要它启动的任何应用程序。其中一个应用程序引发错误的事实是任务计划程序关注的 none。
但是,我能够几乎实现你想要的:你可以在之一上引发错误203动作(不是任务本身)通过确保任务找不到它应该运行作为任务的一部分的程序。
所以我创建了一个 运行s 3 动作的任务:
- MyActualBatch.cmd
- MyExe.exe
- MyResult.cmd
MyBatch.cmd 文件包含以下代码:
@echo off
if exist MyExe.exe del MyExe.exe
if exist result.cmd del result.cmd
set myError=0
:: *********** Do all your processing here **********
:: .....
:: Store your specific error code in %myError%, or to 0 to indicate success e.g.:
set myError=399
:: .....
:: *********** End batch file with following lines **********
if /I %myError% EQU 0 (
:: No error, so let's copy some small executable that the task can actually run
:: without impacting the results of the task
copy /y C:\Windows\System32\ipconfig.exe MyExe.exe > nul
)
:: Create a batch file that will instantly exit using the original error code
:: created in this one
echo exit /b %myError% > result.cmd
exit /b %myError%
(或者,没有使显示混乱的注释):
@echo off
if exist MyExe.exe del MyExe.exe
if exist result.cmd del result.cmd
set myError=399
if /I %myError% EQU 0 (
copy /y C:\Windows\System32\ipconfig.exe MyExe.exe > nul
)
echo exit /b %myError% > result.cmd
exit /b %myError%
每当您的批处理文件需要引发错误时,您将 myError 变量设置为您想要 return 的值(我更喜欢使用我自己的变量而不是依赖于 ERRORLEVEL 系统变量,几乎可以通过任何操作重置它。
在脚本的末尾,一些代码检查 myError 变量是否包含 0(这意味着没有发生错误)。如果是,则批处理创建一个名为 MyExe.exe 的可执行文件,任务将能够在此批处理文件结束后立即 运行。但是如果 myError 包含不同的错误值,那么 MyExe 文件 将不会被创建 ,这将在任务尝试时触发 203 错误运行 下一个。
最后,还创建了一个 Result.cmd 文件,因此它可以 运行 作为任务的最后一个动作,冒泡出你的错误代码已在第一个任务中创建。
注意:MyExe 文件只是 IpConfig.exe 的副本。我选择那个可执行文件只是因为 AFAIK 它在所有 Windows 版本上都可用,它很小,而且它只列出信息,所以它不会通过 运行ning 作为任务的一部分破坏任何东西。
环境: Windows Server 2008 R2
Microsoft Windows Task Scheduler 执行一个 DoThis.cmd 文件,该文件总是 return 事件 201(成功)中的 ERRORLEVEL,即使虽然失败了:
Task Scheduler 成功 完成了任务“\Why Cant This Task Fail”, 实例“{c8655567-5f1c-4eb2-a2e1-91d2cd0405be}”,操作"C:\Windows\SYSTEM32\cmd.exe" ,代码return 399.
我希望 return 如果 ERRORLEVEL 为非零,则事件 202(失败),如果 returned 为零,则事件 201(成功)。
我的批处理文件DoThis.cmd包含
RETURN 399
谢谢, ~肖恩
我认为默认情况下这是不可能的。 任务本身 成功完成,因为它 运行 在预定的时间启动了您想要它启动的任何应用程序。其中一个应用程序引发错误的事实是任务计划程序关注的 none。
但是,我能够几乎实现你想要的:你可以在之一上引发错误203动作(不是任务本身)通过确保任务找不到它应该运行作为任务的一部分的程序。
所以我创建了一个 运行s 3 动作的任务:
- MyActualBatch.cmd
- MyExe.exe
- MyResult.cmd
MyBatch.cmd 文件包含以下代码:
@echo off
if exist MyExe.exe del MyExe.exe
if exist result.cmd del result.cmd
set myError=0
:: *********** Do all your processing here **********
:: .....
:: Store your specific error code in %myError%, or to 0 to indicate success e.g.:
set myError=399
:: .....
:: *********** End batch file with following lines **********
if /I %myError% EQU 0 (
:: No error, so let's copy some small executable that the task can actually run
:: without impacting the results of the task
copy /y C:\Windows\System32\ipconfig.exe MyExe.exe > nul
)
:: Create a batch file that will instantly exit using the original error code
:: created in this one
echo exit /b %myError% > result.cmd
exit /b %myError%
(或者,没有使显示混乱的注释):
@echo off
if exist MyExe.exe del MyExe.exe
if exist result.cmd del result.cmd
set myError=399
if /I %myError% EQU 0 (
copy /y C:\Windows\System32\ipconfig.exe MyExe.exe > nul
)
echo exit /b %myError% > result.cmd
exit /b %myError%
每当您的批处理文件需要引发错误时,您将 myError 变量设置为您想要 return 的值(我更喜欢使用我自己的变量而不是依赖于 ERRORLEVEL 系统变量,几乎可以通过任何操作重置它。
在脚本的末尾,一些代码检查 myError 变量是否包含 0(这意味着没有发生错误)。如果是,则批处理创建一个名为 MyExe.exe 的可执行文件,任务将能够在此批处理文件结束后立即 运行。但是如果 myError 包含不同的错误值,那么 MyExe 文件 将不会被创建 ,这将在任务尝试时触发 203 错误运行 下一个。
最后,还创建了一个 Result.cmd 文件,因此它可以 运行 作为任务的最后一个动作,冒泡出你的错误代码已在第一个任务中创建。
注意:MyExe 文件只是 IpConfig.exe 的副本。我选择那个可执行文件只是因为 AFAIK 它在所有 Windows 版本上都可用,它很小,而且它只列出信息,所以它不会通过 运行ning 作为任务的一部分破坏任何东西。