Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to list scheduled tasks in C#

Before anyone says this is a duplicate, I already checked here, here, here, here, and a couple of other resources, including MS Task Scheduler Class documentation.

I wanted to be able to list the scheduled tasks on my servers using a C# program I´m developing. Some suggested schtasks.exe MS program, others a third-party library, which seems old and working only with .NET Framework 2.0 and others the MS Task Scheduler Class, which seems to be protected and I´m yet to see some example so I understand how I can use it, and others suggested even reading the XML files in each remote machine under C:\Windows\System32\Tasks folder.

My question is: are tasks in Windows that hard to work with using some VS built-in class? Do I have to jump through hoops in order to do something (kind of) silly like listing the tasks already scheduled in a machine?

Thank you,

EDIT:

I ended up using Process class and started schtasks.exe against all servers. Not exactly what I was looking for but it works. If anyone needs the code, just drop me a line and I post it here. Thanks.

like image 975
Adriano_Pinaffo Avatar asked Dec 28 '16 16:12

Adriano_Pinaffo


People also ask

Which command is used to list scheduled jobs?

To view a list of tasks that you scheduled by using the at command, type the at \\computername line, and then press ENTER. To view a specific scheduled task, type the at \\computername id command, and then press ENTER.

How do I export multiple scheduled tasks?

Open Start. Search for Command Prompt, right-click the top result, and select the Run as administrator option. Type the following command to export a scheduled task and press Enter: schtasks /query /xml /tn "\TASK-PATH-TASKSCHEDULER\TASK-NAME" > "%UserProfile%\EXPORT-FOLDER-PATH\TASK-EXPORT-NAME.

How do I get a list of scheduled tasks?

To open Scheduled Tasks, click Start, click All Programs, point to Accessories, point to System Tools, and then click Scheduled Tasks. Use the Search option to search for "Schedule" and choose "Schedule Task" to open the Task Scheduler. Select the "Task Scheduler Library" to see a list of your Scheduled Tasks.

How to get the list of scheduled tasks in Linux?

If you need to use a domain user account to run the task you can specify domainname\username with /RU option. How to get the list of scheduled tasks? Just run Schtasks command and you can see the list of scheduled commands.

How to delete a scheduled task from command line?

We can delete a schedule task using ‘ schtasks /delete /TN task_name ‘ command. For example, to delete the task we created in the example 1 we can run the below command. You can run the below command to delete all the scheduled tasks. There does not seem to be a way to disable a scheduled task from command line.

How to schedule tasks in Windows 7?

‘ Schedule tasks ‘ is a GUI application using which we can schedule tasks, There is an equivalent utility which provides the same functionality but with the advantage that it can be used from windows command line. This command is Schtasks. This is an in-built windows command supported by XP, Vista and Windows 7.

What does the “view scheduled tasks Windows 10” command do?

The “view scheduled tasks windows 10” is a command that allows users to view their scheduled tasks. This will allow users to see what they need to do next and other important information. To open Scheduled Tasks, click Start, click All Programs, point to Accessories, point to System Tools, and then click Scheduled Tasks.


Video Answer


2 Answers

Ah, System.Threading.TaskScheduler is for scheduling work units within a process, not related to the scheduled tasks that you can create from the administrative tools.

I would use a library like TaskScheduler - Googled to find, I do something similar with an awful COM wrapper from 2004 that I've been meaning to update. It makes it pretty easy to see what's in the scheduled tasks.

E.g. from their "Enumerate all tasks" example

void EnumAllTasks()
{
   using (TaskService ts = new TaskService())
      EnumFolderTasks(ts.RootFolder);
}

void EnumFolderTasks(TaskFolder fld)
{
   foreach (Task task in fld.Tasks)
      ActOnTask(task);
   foreach (TaskFolder sfld in fld.SubFolders)
      EnumFolderTasks(sfld);
}

void ActOnTask(Task t)
{
   // Do something interesting here
}
like image 199
dsolimano Avatar answered Sep 22 '22 17:09

dsolimano


In Visual Studio add the reference in COM\Type Libraries: TaskScheduler

and use the Taskschd API: https://docs.microsoft.com/en-us/windows/desktop/api/taskschd

This is an example of the way I'm doing it:

You retrieve the tasks at which you have access permissions, depending if you're running the code with a local or admin account.

My solution was doing this in a Windows Service with Local System account that runs in our servers.

using TaskScheduler;

void ProcessTaskFoler (ITaskFolder taskFolder)
{
    int idx;
    string name, path;
    _TASK_STATE state;

    IRegisteredTaskCollection taskCol = taskFolder.GetTasks((int)_TASK_ENUM_FLAGS.TASK_ENUM_HIDDEN);  // include hidden tasks, otherwise 0
    for (idx = 1; idx <= taskCol.Count; idx++)  // browse al tasks in folder
    {
        IRegisteredTask runTask = taskCol[idx];  // 1 based index

        name = runTask.Name;
        path = runTask.Path;
        state = runTask.State;
        // retrieve other properties...

        Console.WriteLine(path);
    }

    ITaskFolderCollection taskFolderCol = taskFolder.GetFolders(0);  // 0 = reserved for future use
    for (idx = 1; idx <= taskFolderCol.Count; idx++)  // recursively browse subfolders
        ProcessTaskFoler(taskFolderCol[idx]);  // 1 based index
}

void ParseScheduleTasks()
{
    ITaskService taskService = new TaskScheduler.TaskScheduler();
    taskService.Connect();

    ProcessTaskFoler(taskService.GetFolder("\\"));
}
like image 32
Vic_HT Avatar answered Sep 20 '22 17:09

Vic_HT