Windows 脚本 - 静默运行但等待完成/返回正确的代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11659456/
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
Windows script - run silent but wait for completion / return right code
提问by John Humphreys - w00te
Here's my situation:
这是我的情况:
- I have a BAT file that takes a long time to run (1minute to 70 minutes)
- I schedule it with Windows scheduler to run every 10 minutes
- If it schedules again while it is still running, nothing happens (this is good)
- 我有一个需要很长时间才能运行的 BAT 文件(1 分钟到 70 分钟)
- 我用 Windows 调度程序安排它每 10 分钟运行一次
- 如果它仍在运行时再次调度,则不会发生任何事情(这很好)
My problem is that I need my BAT to run silently, and it doesn't. So, I want to launch it with a Windows script like the following:
我的问题是我需要我的 BAT 静默运行,但事实并非如此。所以,我想用如下的 Windows 脚本启动它:
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run chr(34) & "C:\Batch Files\syncfiles.bat" & Chr(34), 0
Set WshShell = Nothing
Unfortunately, when I schedule this script, it does the job but returns instantly, making Windows scheduler think the task is finished when in reality the BAT is just running off by itself somewhere.
不幸的是,当我安排这个脚本时,它完成了工作,但立即返回,使 Windows 调度程序认为任务已经完成,而实际上 BAT 只是在某处自行运行。
Because of this, Windows will reschedule the job again 10 minutes later and make multiple run.
因此,Windows 将在 10 分钟后再次重新安排作业并进行多次运行。
What I need:
我需要的:
Is there a way to tell the Windows script file to wait for the target of the .Run command to complete before progressing/exiting? Basically, I want it to act like I launched another thread and then called join on it, so it does the same thing the BAT would have, but without displaying the console window.
有没有办法告诉 Windows 脚本文件在继续/退出之前等待 .Run 命令的目标完成?基本上,我希望它表现得像我启动了另一个线程,然后在它上面调用了 join,所以它做的事情和 BAT 一样,但不显示控制台窗口。
Other Solutions
其他解决方案
Tell me any other way to get this BAT to execute silently (powershell commands, whatever) and I'll accept it as a solution as well. Just don't tell me to write a full on C++/C# appliation around it, that's overkill :)
告诉我任何其他方式让这个 BAT 静默执行(powershell 命令,无论如何),我也会接受它作为解决方案。只是不要告诉我围绕它编写完整的 C++/C# 应用程序,这太过分了:)
Running:Windows Server 2008 R2
运行:Windows Server 2008 R2
回答by dbenham
I think all you need is TRUE for the optional 3rd argument
我认为对于可选的第三个参数,您需要的只是 TRUE
WshShell.Run chr(34) & "C:\Batch Files\syncfiles.bat" & Chr(34), 0, TRUE
回答by marapet
Here goes the madness...
疯狂来了……
Create a file invisible.vbs
:
创建一个文件invisible.vbs
:
CreateObject("Wscript.Shell").Run """" & WScript.Arguments(0) & """", 0, True
Then in the task scheduler, call your script like this:
然后在任务调度程序中,像这样调用你的脚本:
wscript.exe "C:\Batch Files\invisible.vbs" "C:\Batch Files\syncfiles.bat"
I tested it and this does really work: No command window, and the task is considered running until the batch script ends.
我测试了它,这确实有效:没有命令窗口,并且在批处理脚本结束之前认为任务正在运行。
Credit goes to https://superuser.com/a/62646/40362.
归功于https://superuser.com/a/62646/40362。
For 2008 R2 make invisible.vbs have this content, and just execute it directly. It will run the BAT silently and wait for completion.
2008 R2 make invisible.vbs有这个内容,直接执行就可以了。它将静默运行 BAT 并等待完成。
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run "D:\IUpdateScript.bat", 0, true