schtasks with multiple actions

4

1

I am trying to create a schedule task using command line which has multiple actions running more than one batch files in sequence among other things

SCHTASKS /Create /TN TaskName /SC ONLOGON 
/TR C:\ost\sanAuto.bat
/TR C:\ost\ClEvtLog.cmd

What I have tried that didn't work:

SCHTASKS /Create /TN tsk /SC ONLOGON /TR "C:\sanAuto.bat","C:\ClEvtLog.cmd"

this one create single action to run a program as "C:\sanAuto.bat","C:\ClEvtLog.cmd"

SCHTASKS /Create /TN tsk /SC ONLOGON /TR "C:\sanAuto.bat" /TN "C:\ClEvtLog.cmd"

This one says multiple /TN not allowed

Is there a way to create tasks from command line and specify multiple actions?

Note 1 Exporting and importing the xml is not something i want to do http://iislogs.com/steveschofield/2009/03/20/creating-a-scheduled-task-with-multiple-actions-using-command-line-in-windows-server-2008/

SeanClt

Posted 2016-05-04T16:21:44.430

Reputation: 1 960

1FYI: TR is not "TRigger", it's "Task (to) Run" (a Trigger is what starts the task, which is SC ("Schedule") from the command-line). TN = Task Name, each task can only have one name (your third example). – Ƭᴇcʜιᴇ007 – 2016-05-04T16:36:21.983

Ahh you added your edit while I was writing my answer. If you don't want to use XML importing, then you're pretty much out of luck, at least out of luck using SchTasks to create your task (you may be able to find a 3rd party alternative, personally I know of none). – Ƭᴇcʜιᴇ007 – 2016-05-04T16:45:08.590

Why not just make a script that runs those two other batch script files, and then run that single script with your task? – Ƭᴇcʜιᴇ007 – 2016-05-04T16:47:15.040

i did tried that a batch calling other batch but it just got stuck in the called batch and never went to my main batch, i think i should revisit my batch scripts – SeanClt – 2016-05-04T16:48:34.300

Don't forget about start and/or call when calling batch files from batch files. Also, if you're using Windows 8/Server 2012+ you can also maybe look into using PowerShell to create your tasks. Not sure if it'll let you assign multiple actions or not (off-hand).

– Ƭᴇcʜιᴇ007 – 2016-05-04T16:51:03.713

i did not use start or call if i use start to call a batch do i need to use exit in the called batch? – SeanClt – 2016-05-04T16:53:54.483

Shouldn't have to, but an extra "exit" at the end of a batch usually doesn't hurt.. ;) – Ƭᴇcʜιᴇ007 – 2016-05-04T16:55:02.250

Answers

6

SchTasks does not allow you to define multiple Actions using the TR switch. To define multiple Actions, you need to define your task in an XML file, and then use the XML switch to import the settings.

From schtasks /create /?:

/XML  xmlfile      Creates a task from the task XML specified in a file.
                   Can be combined with /RU and /RP switches, or with /RP
                   alone, when task XML already contains the principal.

The easiest way to define the task in XML is to use Windows' Task Scheduler GUI to create your task (with multiple Actions defined) and then "Export" it to XML.

Task Scheduler GUI

Ƭᴇcʜιᴇ007

Posted 2016-05-04T16:21:44.430

Reputation: 103 763