VBScript用于创建计划任务 [英] VBScript for creating a scheduled task
问题描述
我正在尝试创建一个VBScript,先创建一个批处理文件,然后创建一个计划任务来运行该批处理文件。到目前为止,我尝试过的所有操作都会创建批处理文件,但不会创建计划任务,并且我还没有收到任何错误。这是我到目前为止的内容:
I'm trying to create a VBScript that creates a batch file then creates a scheduled task to run the batch file. So far everything I've tried creates the batch file, but does not create the scheduled task and I haven't received any errors. Here is what I have so far:
Option Explicit
Dim objFSO, outFile, wShell
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set outFile = objFSO.CreateTextFile("C:\test.bat", True)
outFile.WriteLine "Start www.google.com"
outFile.Close
Set wShell = CreateObject ("Wscript.Shell")
wShell.Run "cmd SchTasks /Create /SC WEEKLY /D MON,TUE,WED,THU,FRI /TN 'Test Task' /TR 'C:\test.bat' /ST 16:30", 0
我试过测试任务 和
C:\test.bat
,并得到相同的结果。但是,当我在命令提示符处运行以下命令时:
I've tried ""Test Task""
and ""C:\test.bat""
, and got the same results. But when I run the following command at the command prompt:
SchTasks /Create /SC WEEKLY /D MON,TUE,WED,THU,FRI /TN "Test Task" /TR "C:\test.bat" /ST 16:30
任务创建成功。
我尝试的另一种方法是创建2个批处理文件:一个用于打开网页的批处理文件,以及一个用于创建计划任务的批处理文件。然后,我最后运行了 task.bat
文件。这就是我要这样做的:
Another way I tried this was to create 2 batch files: one batch file to open the webpage, and one batch file to create the scheduled task. Then I concluded with running the task.bat
file at the end. Here's what I had for this:
Option Explicit
Dim objFSO, outFile, wShell
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set outFile = objFSO.CreateTextFile("C:\test.bat", True)
outFile.WriteLine "Start www.google.com"
outFile.Close
Set outFile = objFSO.CreateTextFile("C:\task.bat", True)
outFile.WriteLine "SchTasks /Create /SC WEEKLY /D MON,TUE,WED,THU,FRI /TN ""Test Task"" /TR ""C:\test.bat"" /ST 16:30"
Set wShell = CreateObject ("Wscript.Shell")
wShell.Run "cmd start ""C:\task.bat"""
这将创建批处理文件,但最后只是打开 cmd
,此后什么也不做。
This created the batch files but just opened cmd
at the end and did nothing after that.
我的猜测是问题出在 wShell.Run
部分,但是我没有足够的经验来知道问题出在哪里。
My guess is that the problem lies in the wShell.Run
portion, but I'm not experienced enough to know where the problem lies.
我不确定从这里去哪里,所以任何建议都会很棒。
I'm not sure where to go from here so any suggestions would be great.
不需要 cmd
。 Schtasks
是它自己的可执行文件,而不是 cmd
中的命令。对于引用的参数,只需使用两个引号即可。
There's no need for cmd
. Schtasks
is its own executable, not a command within cmd
. And for your quoted params, just use two quotes.
例如:
wShell.Run "SchTasks /Create /SC WEEKLY /D MON,TUE,WED,THU,FRI /TN ""Test Task"" /TR ""C:\test.bat"" /ST 16:30", 0
这篇关于VBScript用于创建计划任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!