Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Is there an option in possibly SCHTASKS to define where a scheduled job is actually created?

share|improve this question

migrated from stackoverflow.com Jun 20 '11 at 4:14

This question came from our site for professional and enthusiast programmers.

add comment

2 Answers

up vote 2 down vote accepted

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);
share|improve this answer
2  
TaskScheduler is not really a .Net class. It's COM class that you can use from .Net. –  svick Jun 19 '11 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. –  Avada Kedavra Jun 20 '11 at 7:14
    
that's completely different class! Notice that, for example, it doesn't have the Connect() method. The TaskScheduler you use in your code is actually an implementation of the COM interface ITaskService. –  svick Jun 20 '11 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. –  Avada Kedavra Jun 20 '11 at 7:45
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.