dahall / TaskScheduler

Provides a .NET wrapper for the Windows Task Scheduler. It aggregates the multiple versions, provides an editor and allows for localization.
MIT License
1.2k stars 191 forks source link

Get Task Params [] property? #929

Closed josephlugo closed 2 years ago

josephlugo commented 2 years ago

Is there a way to add a new property to Microsoft.Win32.TaskScheduler.Task object, where one can list all the params that were added to execute the Task?

Only way I can think off right now is by manipulating the string "ParamsValue" as shown below:

Microsoft.Win32.TaskScheduler.Task t = TaskService.Instance.GetTask("TaskName"); string ParamsValue = t.Definition.Actions.ToString();

Is there a better way to do this?

Thanks.

dahall commented 2 years ago

What information do you want and in what kind of format?

josephlugo commented 2 years ago
public class Task : IDisposable, IComparable, IComparable<Task>, INotifyPropertyChanged
|
 /// <summary>
 /// Retrieves a list of params used to execute the task.
 /// </summary>
- > public List<string> Params
dahall commented 2 years ago

Thank you. Will you also provide a general idea about what the output would contain?

josephlugo commented 2 years ago

Assuming the following task:

image

// Retrieve the task
Microsoft.Win32.TaskScheduler.Task t = TaskService.Instance.GetTask(taskName);
List<string> taskArguments = t.Actions[0].Params;

taskArguments object will return values: {arg1} and {arg2}

dahall commented 2 years ago

I believe you want to list the parameters specified for actions that run programs. If that is correct, the following code should do it:

var t = TaslService.Instace.GetTask(taskName);
foreach (var action in t.Actions)
{
   if (action is ExecAction exe && exe.Params != null)
      Console.WriteLine(exe.Params);
}

Part of the challenge here is that a task can have multiple actions of different types. Using Linq, you could also do the following in one line to get the first action with params (or null otherwise):

return TaslService.Instace.GetTask(taskName)?.Actions.OfType<ExecAction>().FirstOrDefault()?.Params;
josephlugo commented 2 years ago

Exactly,

I was able to do so, although had to update a little your suggested code to:

string taskParamsValue = TaskService.Instance.GetTask(taskName)?.Definition.Actions.OfType<ExecAction>().FirstOrDefault()?.Arguments;

Thanks again!