VBScript to create a scheduled task

I am trying to create a VBScript that creates a batch file and then creates a scheduled task to run a batch file. So far, everything I tried creates a batch file, but does not create a scheduled task, and I have not received any errors. Here is what I still have:

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

I tried ""Test Task""and ""C:\test.bat""got the same results. But when I run the following command on the command line:

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

The task is created successfully.

In another way, I tried to create 2 batch files: one batch file to open a web page and one batch file to create a scheduled task. Then I finished working with the file task.batat the end. Here is what I did 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 .

, wShell.Run, , , .

, , .

+4
3

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
+4

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

, , .

()

, , Notepad . , , , , Notepad, , .

, .

  • TaskService. .

  • . TaskService.GetFolder, , , TaskService.NewTask TaskDefinition, .

  • TaskDefinition. TaskDefinition.Settings, , , TaskDefinition.RegistrationInfo, , .
  • , , TaskDefinition.Triggers. TriggerCollection. TriggerCollection.Create ( , ), , . , , . , .
  • TaskDefinition.Actions. ActionCollection. ActionCollection.Create, , . ExecAction, , .
  • TaskFolder.RegisterTaskDefinition. Notepad 30 .

VBScript , Notepad 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
+3

In the script, you used single quotes in the command, and in your test in CMD, you used double quotes. Will it work if you run ..?:

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

If not, try with double quotes and avoid them by doubling them:

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

Source: https://habr.com/ru/post/1598958/


All Articles