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
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
The task gets created successfully.
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"""
This created the batch files but just opened cmd
at the end and did nothing after that.
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.
schtasks
does not provide all options the GUI does. TheSchedule.Service
object allows more complete control over task properties. – Remission