创建 SCHTASK.EXE 任务以运行带参数的批处理文件的语法

创建 SCHTASK.EXE 任务以运行带参数的批处理文件的语法

我有一组较旧的批处理文件,用于安排我负责的数据库的一些维护任务的运行。它们使用了 AT 命令。数据库已移至 Windows Server 2016,其中已弃用 AT 命令。我想改用 SCHTASKS.EXE,但遇到了一些挑战。

这组维护任务分为三个单独的批处理文件 Ibak1.bat、Ibak2.bat 和 Ibak3.bat。

还有一个 Ibak0.bat,它安排 Ibak1 到 Ibak3 批处理文件中的每一个在不同的时间运行。

Ibak0.bat 将一组参数传递给 Ibak1、Ibak2 和 Ibak3。不过,为了回答我的问题,我们可以只关注 Ibak0.bat 和 Ibak1.bat。Ibak2 和 Ibak3 的工作方式相同,但参数类型不同。

我的问题是:

问题 1:可以安排批处理文件与 SCHTASKS.EXE 一起运行吗?

Q2:使用 AT 命令,您必须指定要运行的目标批处理文件,如下所示:

at %6 /interactive /every:M,T,W,Th,F,S,Su cmd /c "%5Ibak1.bat %1 %2 %3 %4 "

在调度批处理文件时是否也需要使用 SCHTASKS 指定“cmd /c”?

问题 3:我何时应该使用 /IT 参数?除了将输出回显到日志文件之外,所有批处理文件都不需要用户交互。

Q4:如您所见,从 Ibak0.bat 到 Ibak1.bat 传递了许多参数。传递参数的语法是什么?

以下是我尝试过的所有语法变体。我无法创建计划任务,或者在某些情况下(尝试 3、10 和 14),我成功创建了任务,命令窗口闪烁了一瞬间,但没有任何迹象表明任务运行正常。

 Atempt   1: SchTasks /CREATE /TN Ibak1.Make_Copy_%1 /TR "'%5Ibak1.bat' '%1' '%2' '%3' '%4' "        /ST %6 /IT /SC DAILY  Outcome: invalid argument         
 Atempt   2: SchTasks /CREATE /TN Ibak1.Make_Copy_%1 /TR   %5Ibak1.bat %1 %2 %3 %4                   /ST %6 /IT /SC DAILY  Outcome: invalid argument         
 Atempt   3: SchTasks /CREATE /TN Ibak1.Make_Copy_%1 /TR  "%5Ibak1.bat %1 %2 %3 %4"                  /ST %6 /IT /SC DAILY  Outcome: scheduled but did not run. cmd opens but closes immediately.         
 Atempt   4: SchTasks /CREATE /TN Ibak1.Make_Copy_%1 /TR '"%5Ibak1.bat" %1 %2 %3 %4'                 /ST %6 /IT /SC DAILY  Outcome: can't remember         
 Atempt   5: SchTasks /CREATE /TN Ibak1.Make_Copy_%1 /TR  "%5Ibak1.bat" %1 %2 %3 %4                  /ST %6 /IT /SC DAILY  Outcome: invalid argument         
 Atempt   6: SchTasks /CREATE /TN Ibak1.Make_Copy_%1 /TR  "%5Ibak1.bat" /%1 /%2 /%3 /%4              /ST %6 /IT /SC DAILY  Outcome: invalid argument
 Atempt   7: SchTasks /CREATE /TN Ibak1.Make_Copy_%1 /TR  "%5Ibak1.bat" \%1 \%2 \%3 \%4              /ST %6 /IT /SC DAILY  Outcome: invalid argument
 Atempt   8: SchTasks /CREATE /TN Ibak1.Make_Copy_%1 /TR cmd /c "%5Ibak1.bat %1 %2 %3 %4   "         /ST %6 /IT /SC DAILY  Outcome: invalid argument  
 Atempt   9: SchTasks /CREATE /TN Ibak1.Make_Copy_%1 /TR cmd "%5Ibak1.bat %1 %2 %3 %4   "            /ST %6 /IT /SC DAILY  Outcome: invalid argument  
 Attempt 10: SchTasks /CREATE /TN Ibak1.Make_Copy_%1 /TR "cmd.exe /c \"%5Ibak1.bat %1 %2 %3 %4 \" "  /ST %6 /IT /SC DAILY  Outcome: scheduled but did not run. cmd opens but closes immediately.    
 Attempt 11: SchTasks /CREATE /TN Ibak1.Make_Copy_%1 /TR "cmd.exe /k \"%5Ibak1.bat %1 %2 %3 %4 \" "  /ST %6 /IT /SC DAILY  Outcome: C:\TNS not recognized as a program    
 Attempt 12: SchTasks /CREATE /TN Ibak1.Make_Copy_%3 /TR c:\TNS\(data)\IBAK\Ibak1.bat %1 %2 %9       /ST %6 /IT /SC DAILY  Outcome: Invalid argument/option 
 Attempt 13: SchTasks /CREATE /TN Ibak1.Make_Copy_%3 /TR "c:\TNS\(data)\IBAK\Ibak1.bat %1 %2 %9 "    /ST %6 /IT /SC DAILY  Outcome: ERROR: The parameter is incorrect.
 Attempt 14: SchTasks /CREATE /TN Ibak1.Make_Copy_%3 /TR "cmd.exe /k \"c:\TNS\(data)\IBAK\Ibak1.bat %1 %2 %9 \" " /ST %6 /IT /SC DAILY Outcome: C:\TNS not recognized as a program

关于参数的一些重要信息:

 Parameters : %1 Live database file being copied.
 Attempts     %2 Path to database file. Include terminating "\".
 1 to 11      %3 Server Volume or share name to receive copy of DB file being backed up. Do *NOT* Include terminating "\".
              %4 Server Path to copy %2%1 to. Include terminating "\" but no drive letter.
              %5 Path to batch file collection. Include terminating "\" and drive letter.
              %6 Time Ibak1.bat is scheduled to run. Use 24 hour format.
              %7 Time Ibak2.bat is scheduled to run. Use 24 hour format.
              %8 Time Ibak3.bat is scheduled to run. Use 24 hour format. 
              

因为我认为在第 12 次尝试之后将文件夹名称与文件名分开传递可能会出现一些问题,所以我使用了文件的完整路径名。下面是我重新排列参数的方式:

 Parameters : %1 Live FDB file being copied (full path).
 Attempts     %2 Live FDB file copy target (full path). 
 12 onwards   %3 FDB identifier (ex.: WILSONS, VTT, SMITH)
              %4 RESERVED
              %5 RESERVED
              %6 Time ibak1.bat is scheduled to run. Use 24 hour format.
              %7 Time ibak2.bat is scheduled to run. Use 24 hour format.
              %8 Time ibak3.bat is scheduled to run. Use 24 hour format.
              %9 Log file name (full path).

相关内容