2015-07-21 116 views
4

我试图创建一个创建批处理文件的VBScript,然后创建一个计划任务来运行该批处理文件。到目前为止,我尝试过的所有内容都会创建批处理文件,但不会创建计划任务,并且我还没有收到任何错误。以下是我迄今为止:用于创建计划任务的VBScript

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 

我试过""Test Task""""C:\test.bat"",并得到了相同的结果。但是,当我在命令提示符处运行以下命令时:

SchTasks /Create /SC WEEKLY /D MON,TUE,WED,THU,FRI /TN "Test Task" /TR "C:\test.bat" /ST 16:30 

任务已成功创建。

我试过这个的另一种方法是创建2个批处理文件:一个批处理文件打开网页,一个批处理文件创建计划任务。然后我结束时运行task.bat文件。下面是我有什么这样的:

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,之后什么也没做。

我的猜测是问题在于wShell.Run部分,但我没有足够的经验来知道问题出在哪里。

我不知道该从哪里出发,所以任何建议都会很棒。

回答

4

不需要cmdSchtasks是它自己的可执行文件,而不是cmd内的命令。而对于你引用的参数,只需使用两个引号。

例如:

wShell.Run "SchTasks /Create /SC WEEKLY /D MON,TUE,WED,THU,FRI /TN ""Test Task"" /TR ""C:\test.bat"" /ST 16:30", 0 
+0

要小心,虽然,'schtasks'不提供GUI做的所有选项。 'Schedule.Service'对象允许更完整地控制任务属性。 –

+0

工作正常!我知道这很简单,我忽略了。感谢您的帮助。 – takeitback

1

在脚本中,您在命令中使用单引号,而在CMD测试中,使用双引号。如果你运行它是否仍然有效..:?

SchTasks /Create /SC WEEKLY /D MON,TUE,WED,THU,FRI /TN 'Test Task' /TR 'C:\test.bat' /ST 16:30 

如果没有,请尝试使用双引号,并通过他们加倍逃避他们:

wShell.Run "SchTasks /Create /SC WEEKLY /D MON,TUE,WED,THU,FRI /TN ""Test Task"" /TR ""C:\test.bat"" /ST 16:30", 0 
3

作为一个VBScript你可以做任何一个命令可以做。

以下是如何在根文件夹中列出计划任务的方法。

Set TS = CreateObject("Schedule.Service") 
TS.Connect("Serenity") 

Set rootFolder = TS.GetFolder("\") 

Set tasks = rootFolder.GetTasks(0) 

If tasks.Count = 0 Then 
    Wscript.Echo "No tasks are registered." 
Else 
    WScript.Echo "Number of tasks registered: " & tasks.Count 

    For Each Task In Tasks 
    A=Task.Name 
    A = A & " " & Task.NextRunTime 
    A = A & " " & Task.LastTaskResult 
    wscript.echo A 
    Next 
End If 

这是从documentation显示如何创建任务。

时间触发示例(脚本)

此脚本示例演示如何创建一个在特定时间运行记事本的任务。该任务包含一个基于时间的触发器,用于指定启动任务的开始边界,一个运行记事本的可执行操作以及一个停用任务的结束边界。

下面的过程介绍了如何安排任务在特定时间启动一个可执行文件。

要安排记事本在特定时间

  1. 创建TaskService对象开始。该对象允许您在指定的文件夹中创建任务。

  2. 获取任务文件夹并创建任务。使用TaskService.GetFolder方法获取存储任务的文件夹,并使用TaskService.NewTask方法创建代表任务的TaskDefinition对象。

  3. 使用TaskDefinition对象定义有关任务的信息。使用TaskDefinition.Settings属性定义确定Task Scheduler服务如何执行任务的设置,并定义描述该任务的信息的TaskDefinition.RegistrationInfo属性。
  4. 使用TaskDefinition.Triggers属性创建基于时间的触发器。此属性提供对TriggerCollection对象的访问。使用TriggerCollection.Create方法(指定要创建的触发器的类型)创建基于时间的触发器。在创建触发器时,设置触发器的起始边界和结束边界以激活和停用触发器。起始边界指定何时执行任务的动作。
  5. 使用TaskDefinition.Actions属性为要执行的任务创建一个操作。此属性提供对ActionCollection对象的访问。使用ActionCollection.Create方法指定要创建的操作的类型。本示例使用ExecAction对象,该对象表示执行命令行操作的操作。
  6. 使用方法TaskFolder.RegisterTaskDefinition注册任务。对于这个例子,任务将在当前时间加上30秒启动记事本。

以下VBScript示例显示了如何安排任务在注册任务30秒后执行记事本。

' This sample schedules a task to start notepad.exe 30 seconds 
' from the time the task is registered. 
'------------------------------------------------------------------ 

' A constant that specifies a time-based trigger. 
const TriggerTypeTime = 1 
' A constant that specifies an executable action. 
const ActionTypeExec = 0 

'******************************************************** 
' Create the TaskService object. 
Set service = CreateObject("Schedule.Service") 
call service.Connect() 

'******************************************************** 
' Get a folder to create a task definition in. 
Dim rootFolder 
Set rootFolder = service.GetFolder("\") 

' The taskDefinition variable is the TaskDefinition object. 
Dim taskDefinition 
' The flags parameter is 0 because it is not supported. 
Set taskDefinition = service.NewTask(0) 

'******************************************************** 
' Define information about the task. 

' Set the registration info for the task by 
' creating the RegistrationInfo object. 
Dim regInfo 
Set regInfo = taskDefinition.RegistrationInfo 
regInfo.Description = "Start notepad at a certain time" 
regInfo.Author = "Administrator" 

' Set the task setting info for the Task Scheduler by 
' creating a TaskSettings object. 
Dim settings 
Set settings = taskDefinition.Settings 
settings.Enabled = True 
settings.StartWhenAvailable = True 
settings.Hidden = False 

'******************************************************** 
' Create a time-based trigger. 
Dim triggers 
Set triggers = taskDefinition.Triggers 

Dim trigger 
Set trigger = triggers.Create(TriggerTypeTime) 

' Trigger variables that define when the trigger is active. 
Dim startTime, endTime 

Dim time 
time = DateAdd("s", 30, Now) 'start time = 30 seconds from now 
startTime = XmlTime(time) 

time = DateAdd("n", 5, Now) 'end time = 5 minutes from now 
endTime = XmlTime(time) 

WScript.Echo "startTime :" & startTime 
WScript.Echo "endTime :" & endTime 

trigger.StartBoundary = startTime 
trigger.EndBoundary = endTime 
trigger.ExecutionTimeLimit = "PT5M" 'Five minutes 
trigger.Id = "TimeTriggerId" 
trigger.Enabled = True 

'*********************************************************** 
' Create the action for the task to execute. 

' Add an action to the task to run notepad.exe. 
Dim Action 
Set Action = taskDefinition.Actions.Create(ActionTypeExec) 
Action.Path = "C:\Windows\System32\notepad.exe" 

WScript.Echo "Task definition created. About to submit the task..." 

'*********************************************************** 
' Register (create) the task. 

call rootFolder.RegisterTaskDefinition(_ 
    "Test TimeTrigger", taskDefinition, 6, , , 3) 

WScript.Echo "Task submitted." 

'------------------------------------------------------------------ 
' Used to get the time for the trigger 
' startBoundary and endBoundary. 
' Return the time in the correct format: 
' YYYY-MM-DDTHH:MM:SS. 
'------------------------------------------------------------------ 
Function XmlTime(t) 
    Dim cSecond, cMinute, CHour, cDay, cMonth, cYear 
    Dim tTime, tDate 

    cSecond = "0" & Second(t) 
    cMinute = "0" & Minute(t) 
    cHour = "0" & Hour(t) 
    cDay = "0" & Day(t) 
    cMonth = "0" & Month(t) 
    cYear = Year(t) 

    tTime = Right(cHour, 2) & ":" & Right(cMinute, 2) & _ 
     ":" & Right(cSecond, 2) 
    tDate = cYear & "-" & Right(cMonth, 2) & "-" & Right(cDay, 2) 
    XmlTime = tDate & "T" & tTime 
End Function 
+1

请不要引用外部来源。 –

+0

我说_From help_。这不是我的错,你不会使用它。还要注意生成日期,它确切指定它来自哪个版本的帮助。并且我在过去的9年中发布了这种数千次的内容 - **下载并安装Windows SDK ** http://msdn.microsoft.com/en-us/windows/hardware/hh852363 – bill

+0

对(未指定的)离线文档并不是非常有用,尤其是当在线文档很容易找到时。 –