Task Scheduler Managed Wrapper does not show all tasks
Asked Answered
L

2

6

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;
    }
}
Landmeier answered 10/5, 2016 at 11:44 Comment(3)
Well, which tasks are hidden? Maybe those of another user?Catalyze
@Catalyze You are right. The problem seems to be that the tasks are run by another user than the one which is running the code. Thank you!Landmeier
taskscheduler.codeplex.com not foundClapp
L
3

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

Landmeier answered 11/5, 2016 at 7:46 Comment(3)
If your app runs as an administrator you might be able to see them.Bucella
I just had this same problem, but .FindAllTasks(new RegEx(.*)) was not finding them either (was testing this in LINQPad). I had to run LINQPad as Administrator before .FindAllTasks(...) would return the tasks I was looking for.Homemade
The FindAllTasks method with any value for its parameter will find only find the tasks which the system determines the current account has the right to see. There are actually ACLs on each task written by default at the time of registration. As noted in this thread, to see all tasks, you have to have all rights, i.e. admin status.Downbow
H
8

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);
                }
Harvin answered 25/1, 2017 at 20:28 Comment(1)
Microsoft.Win32.TaskScheduler For Net Framework, and for Net Core (Net 5, 6,...) too?Clapp
L
3

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

Landmeier answered 11/5, 2016 at 7:46 Comment(3)
If your app runs as an administrator you might be able to see them.Bucella
I just had this same problem, but .FindAllTasks(new RegEx(.*)) was not finding them either (was testing this in LINQPad). I had to run LINQPad as Administrator before .FindAllTasks(...) would return the tasks I was looking for.Homemade
The FindAllTasks method with any value for its parameter will find only find the tasks which the system determines the current account has the right to see. There are actually ACLs on each task written by default at the time of registration. As noted in this thread, to see all tasks, you have to have all rights, i.e. admin status.Downbow

© 2022 - 2024 — McMap. All rights reserved.