【问题标题】:VBScript for creating a scheduled task用于创建计划任务的 VBScript
【发布时间】:2015-10-11 12:25:12
【问题描述】:

我正在尝试创建一个 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 部分,但我没有足够的经验知道问题出在哪里。

我不确定从这里去哪里,所以任何建议都会很棒。

【问题讨论】:

    标签: windows batch-file vbscript cmd windows-task-scheduler


    【解决方案1】:

    作为一个 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 属性定义确定任务计划程序服务如何执行任务的设置,使用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
    

    【讨论】:

    • 请不要在没有参考的情况下引用外部资源。
    • 我说来自帮助。你不使用它不是我的错。还要注意确切指定它来自哪个版本的帮助的构建日期。我在过去 9 年中发布了数千次 - 下载并安装 Windows SDK msdn.microsoft.com/en-us/windows/hardware/hh852363
    • 对(未指定的)离线文档的引用并没有太大帮助,尤其是当在线文档很难找到时。
    • 我们在一个 VBScript 组中。程序员应该安装帮助。我也希望他们可以学习如何使用谷歌。
    【解决方案2】:

    不需要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
    

    【讨论】:

    • 但请注意,schtasks 并未提供 GUI 提供的所有选项。 Schedule.Service 对象允许更完整地控制任务属性。
    【解决方案3】:

    在脚本中,您在命令中使用了单引号,而在 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
    

    【讨论】:

      猜你喜欢
      • 2023-04-08
      • 1970-01-01
      • 2011-09-26
      • 2011-11-15
      • 1970-01-01
      • 1970-01-01
      • 2012-03-02
      • 2015-03-14
      • 1970-01-01
      相关资源
      最近更新 更多