windows 在 win 7 任务调度程序中按顺序运行批处理文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4391768/
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-09 07:51:19  来源:igfitidea点击:

Running batch files sequentially in win 7 task scheduler

windowstaskscheduler

提问by Jim

I have 2 batch files as "actions" in a single task. The first one does a scan for viruses using microsoft security essentials. The second one puts the computer to sleep. The problem is that when the task is run, it seems like both batch files run simultaneously, that is, I can see the start of the virus scan, but then the computer goes to sleep almost immediately, so the scan really never has a chance to start. I understood that batch files would run in sequential order in the task scheduler. Am I wrong? How do I make the sleep batch file wait until the scan batch file has completed? Here are the 2 simple batch files...

我在单个任务中有 2 个批处理文件作为“操作”。第一个使用 microsoft security Essentials 对病毒进行扫描。第二个使计算机进入睡眠状态。问题是任务运行的时候,好像两个批处理文件同时运行,也就是说,我可以看到病毒扫描的开始,但是计算机几乎立即进入睡眠状态,所以扫描真的没有机会开始。我知道批处理文件将在任务调度程序中按顺序运行。我错了吗?如何让睡眠批处理文件等待扫描批处理文件完成?这是两个简单的批处理文件...

Batch file one

批处理文件一

"C:\Program Files\Microsoft Security Essentials\MpCmdRun.exe" -scan -scantype 1

"C:\Program Files\Microsoft Security Essentials\MpCmdRun.exe" -scan -scantype 1

Batch file two

批处理文件二

powercfg -h off

powercfg -h 关闭

rundll32.exe powrprof.dll,SetSuspendState 0,1,0

rundll32.exe powrprof.dll,SetSuspendState 0,1,0

powercfg -h on

powercfg -h 开启

回答by Rui Marques

It's possible using Windows Scheduled Tasks for Windows 7, Windows Server 2008 R2, Windows Server 2012, Windows Vista.

可以使用适用于 Windows 7、Windows Server 2008 R2、Windows Server 2012、Windows Vista 的 Windows 计划任务。

From Microsoft documentation:

来自微软文档

A task action is the work that is performed when the task is run. A task can have a single action or a maximum of 32 actions. Each action contains settings that determine how the action is performed. A task's actions are displayed on the Actions tab of the Task Properties or Create Task dialog box. When multiple actions are specified, they are executed in sequential orderstarting with the action at the top of the list in the Actions tab...

任务操作是在任务运行时执行的工作。一个任务可以有一个动作或最多 32 个动作。每个操作都包含确定操作执行方式的设置。任务的操作显示在“任务属性”或“创建任务”对话框的“操作”选项卡上。当指定多个动作时,它们将按顺序执行,从动作选项卡中列表顶部的动作开始...

回答by LeoB

I believe the easiest way to fix this is to have the first batch file call the second batch file. Since the commands in batch files are processed sequentially that would fix your problem.

我相信解决这个问题的最简单方法是让第一个批处理文件调用第二个批处理文件。由于批处理文件中的命令是按顺序处理的,因此可以解决您的问题。

回答by Brian

Another way is for the action to contain START /W C:\MyScript.batThe /Wis a parameter for the start command to wait for completion before returning control.

另一种方式是遏制行动START /W C:\MyScript.bat/ W是start命令等待完成返回控制之前的参数。