Is there an option in possibly SCHTASKS to define where a scheduled job is actually created?
2 Answers
Instead of calling the SCHTASKS
I would recommend to use the .Net wrapper around the COM class TaskScheduler
, throught which you can interact with the Windows Task Scheduler. It required a bit more code but offers a rich set of properties and good control of the tasks. Doing so can use ITaskFolder
to create folders for your task(s). I include som of the code I use below (which will create a task called "MyTaskName" in the folder "MyTaskFolder". Also check out this article for good information on the subject.
TaskScheduler.TaskScheduler scheduler = new TaskScheduler.TaskScheduler();
scheduler.Connect(null, null, null, null); //run as current user.
ITaskDefinition taskDef = scheduler.NewTask(0);
taskDef.RegistrationInfo.Author = task.TaskAuthor;
...
ITimeTrigger trigger = (ITimeTrigger)taskDef.Triggers.Create(_TASK_TRIGGER_TYPE2.TASK_TRIGGER_TIME);
...
IExecAction action = (IExecAction)taskDef.Actions.Create(_TASK_ACTION_TYPE.TASK_ACTION_EXEC);
...
ITaskFolder root = scheduler.GetFolder("\\");
root.CreateFolder("MyTaskFolder"); //// here
IRegisteredTask regTask = root.RegisterTaskDefinition(
"MyTaskName",
taskDef,
(int)_TASK_CREATION.TASK_CREATE_OR_UPDATE,
null, // user
null, // password
_TASK_LOGON_TYPE.TASK_LOGON_INTERACTIVE_TOKEN, //User must already be logged on. The task will be run only in an existing interactive session.
"" //SDDL
);
Note that this will throw an exception if the folder already exists. Ypu can get the available folders using
ITaskFolderCollection folders = root.GetFolders(0);
-
2
TaskScheduler
is not really a .Net class. It's COM class that you can use from .Net.– svickJun 19, 2011 at 23:21 -
MSDN expresses it as a class, look at the topic "TaskScheduler Class" of this article: msdn.microsoft.com/en-us/library/… . You might be rights that its only a wrapper around a COM object, but I don't think its that obvious whether it is a class or not. Jun 20, 2011 at 7:14
-
that's completely different class! Notice that, for example, it doesn't have the
Connect()
method. TheTaskScheduler
you use in your code is actually an implementation of the COM interfaceITaskService
.– svickJun 20, 2011 at 7:41 -
Aoouch, you are so right, im so wrong. Linking to the wrong class is embarrassing, at least... :( I'll update the answer to reflect on your input. And +1 to you. Jun 20, 2011 at 7:45
Simply use a path name in the task name string (/TN
parameter). For instance, /TN Foo\Bar
will create a task named Bar in the Foo folder.