Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Task Scheduler Managed Wrapper does not show all tasks

I have created a simple Windows Forms Application that displays the names and folders of all scheduled tasks on the machine. I'm using the Task Scheduler Managed Wrapper (Microsoft.Win32.TaskScheduler) and below is the code that gets the names and display them. However it seems as if AllTasks does not actually give me all tasks. There are some that are not displayed. What could cause a task to be hidden in this case?

using (TaskService tsksrvs = new TaskService())
{
    foreach (Task tsk in tsksrvs.AllTasks)
    {
        textJobsList.Text += tsk.Name + " (" + tsk.Folder + ")" + Environment.NewLine;
    }
}
like image 906
jahrentorp Avatar asked May 10 '16 11:05

jahrentorp


People also ask

How do I view all Task Scheduler?

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 do I find hidden scheduled tasks?

By default, hidden tasks are not shown in the Task Scheduler user interface. You can view hidden tasks when Show Hidden Tasks is selected in the View menu. You make a task hidden when you click the Hidden check box on the General tab of the Task Properties or Create Task dialog box.

Are scheduled tasks stored in registry?

Yes, I know that the scheduled tasks are stored in the registry (HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Schedule...) and the mentioned folder with the task is visible there.


2 Answers

The property AllTasks of the object Microsoft.Win32.TaskScheduler.TaskService returns only the Windows Scheduler tasks where the task's .Definition.Principal.UserId is either a user running the program Or System Or NETWORK SERVICE Or LOCAL SERVICE Or empty. By the way, it does not matter what value the property Definition.RegistrationInfo.Author has.

If you need to get ALL the task for ALL the users, you can accomplish it by the following code:

using Microsoft.Win32.TaskScheduler;
using System.Diagnostics;
using System.Text.RegularExpressions;
......................................
                Task[] allTasks = TaskService.Instance.FindAllTasks(new Regex(".*")); // this will list ALL tasks for ALL users
                foreach (Task tsk in allTasks)
                {
                    //Do whatever you need here, for example:
                    Debug.WriteLine("TaskName:{0}; Path:{1}; Author:{2}; Principal: {3}; ", tsk.Name, tsk.Path, tsk.Definition.RegistrationInfo.Author, tsk.Definition.Principal.UserId);
                }
like image 86
Arkady Yampolsky Avatar answered Sep 29 '22 07:09

Arkady Yampolsky


As @nvoigt pointed out above the tasks not visible are running as a different user than the one executing the code.

like image 45
jahrentorp Avatar answered Sep 29 '22 08:09

jahrentorp