tak*_*ack 6 windows vbscript cmd batch-file schtasks
我正在尝试创建一个创建批处理文件的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
Run Code Online (Sandbox Code Playgroud)
我试过""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
Run Code Online (Sandbox Code Playgroud)
任务成功创建.
我尝试这种方法的另一种方法是创建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"""
Run Code Online (Sandbox Code Playgroud)
这创建了批处理文件,但最后才打开cmd,之后什么也没做.
我的猜测是问题在于wShell.Run部分,但我没有足够的经验知道问题所在.
我不知道从哪里去,所以任何建议都会很棒.
小智 7
作为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
Run Code Online (Sandbox Code Playgroud)
这来自显示如何创建任务的文档.
时间触发示例(脚本)
此脚本示例显示如何创建在特定时间运行记事本的任务.该任务包含一个基于时间的触发器,它指定激活任务的起始边界,一个运行记事本的可执行操作,以及一个停用该任务的结束边界.
以下过程描述如何安排任务在特定时间启动可执行文件.
安排记事本在特定时间开始
创建一个TaskService对象.此对象允许您在指定的文件夹中创建任务.
获取任务文件夹并创建任务.使用该
TaskService.GetFolder方法获取存储任务的文件夹以及TaskService.NewTask创建表示任务的TaskDefinition对象的方法.- 使用
TaskDefinition对象定义有关任务的信息.使用该TaskDefinition.Settings属性可以定义用于确定Task Scheduler服务如何执行任务的设置,以及TaskDefinition.RegistrationInfo用于定义描述任务的信息的属性.- 使用
TaskDefinition.Triggers属性创建基于时间的触发器.此属性提供对TriggerCollection对象的访问.使用该TriggerCollection.Create方法(指定要创建的触发器类型)来创建基于时间的触发器.在创建触发器时,设置触发器的起始边界和结束边界以激活和取消激活触发器.起始边界指定何时执行任务的操作.- 使用该
TaskDefinition.Actions属性为要执行的任务创建操作.此属性提供对ActionCollection对象的访问.使用此ActionCollection.Create方法指定要创建的操作类型.此示例使用一个ExecAction对象,该对象表示执行命令行操作的操作.- 使用该
TaskFolder.RegisterTaskDefinition方法注册任务.对于此示例,任务将在当前时间加30秒启动记事本.以下VBScript示例显示如何在注册任务30秒后安排任务执行记事本.
Run Code Online (Sandbox Code Playgroud)' 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
不需要cmd。Schtasks是其自己的可执行文件,而不是中的命令cmd。对于引用的参数,只需使用两个引号即可。
例如:
wShell.Run "SchTasks /Create /SC WEEKLY /D MON,TUE,WED,THU,FRI /TN ""Test Task"" /TR ""C:\test.bat"" /ST 16:30", 0
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
17752 次 |
| 最近记录: |