2

Is it possible to create a task using schtasks, such that this task runs at specific times depending on the configuration provided by the user?

Ideally what I want to achieve is this - a user enters the specific times s/he wants the task executed (e.g. generating a report and having it emailed). This string of specific times is comma-delimited, e.g. 08:00, 11:20, 14:00, 17:59 or 09:15, 10:30, 11:45, 13:00.

Currently all I know is that schtasks /create allow you to specify a start time using the /st <hh:mm> but that's only for one specific timing.

ohseekay
  • 795
  • 3
  • 20
  • 37

1 Answers1

1

You can set the starting time for tasks and set the interval at which it repeats, but that's about it for individual tasks.

If you want to create a single task that runs every 3 hours, starting at 09:00, you could do something like this:
Schtasks /Create /TN example /TR C:\example\report.bat /SC DAILY /ST 09:00 /DU 300 /RI 180
Notice that i set /DU: it's a requirement of schtasks.exe that the duration is more than the run interval.

If you wanted to create several tasks to run at different times, I'd either add one at a time or have them each on a single line of a file that is then passed through a batch file (which is effectively the same). Once you get into delimited lists it gets messy; I haven't found a pretty way to extract data from them yet. It's certainly possible, but feels like the kind of thing that people tell you is "bad practice".
To add several tasks in succession, I'd create a batch file that looked something like this:

set i=1

:top

set /P input=if you want to create a scheduled task, enter the time you want in HH:MM format. otherwise, type "q" : 
if (%input%) EQU (q) exit /B 0

Schtasks /Create /TN %username%\%i% /TR C:\example\report.bat /SC DAILY /ST %input% /RU %username%

set /A i+=1

goto top

The tasks created are numbers starting at one and counting up to infinity (as far as the user goes) and filed under a folder titled the same as the username of the person who ran the script. Technically, the name you access them by would be "%username%\1", "%username%\2". This is so that multiple users can use the same script to make multiple tasks without the danger of overwriting each others tasks.
The script can be stopped at any time by entering "q" by itself.
Potentially, you could input a file to this script with one time on each line and a q at the end, and it would run through the entire list and exit.

Evan
  • 566
  • 1
  • 6
  • 15