Hello,
I need to create a task whose user account is a group, specifically that is the group of Users, and that is executed only when the user has logged in. But when I indicate the group, when I register the task, the access denied message appears. Thanks in advance!
The code that I have implemented is the following :
using (TaskService ts = new TaskService())
{
// Create a new task definition and assign properties
TaskDefinition td = ts.NewTask();
td.RegistrationInfo.Description = "Control horas Grupo Fes";
td.RegistrationInfo.Author = Environment.UserName;
td.Principal.GroupId = "BUILTIN\\Usuarios";
td.Principal.Id = "Author";
td.Settings.AllowDemandStart = true;
td.Settings.AllowHardTerminate = false;
td.Settings.Compatibility = TaskCompatibility.V2_3;
td.Settings.DisallowStartIfOnBatteries = false;
td.Settings.DisallowStartOnRemoteAppSession = false;
td.Settings.MultipleInstances = TaskInstancesPolicy.IgnoreNew;
td.Settings.RestartCount = 3;
td.Settings.RestartInterval = TimeSpan.FromMinutes(2);
td.Settings.RunOnlyIfIdle = false;
td.Settings.ExecutionTimeLimit = TimeSpan.FromDays(0);
td.Settings.RunOnlyIfNetworkAvailable = false;
td.Settings.StartWhenAvailable = true;
td.Settings.StopIfGoingOnBatteries = false;
td.Settings.UseUnifiedSchedulingEngine = false;
td.Settings.Volatile = false;
td.Settings.WakeToRun = false;
td.Settings.Hidden = true;
td.Settings.IdleSettings.RestartOnIdle = false;
td.Settings.IdleSettings.StopOnIdleEnd = true;
td.Settings.IdleSettings.WaitTimeout = TimeSpan.FromSeconds(0);
DailyTrigger _triggerDaily = new DailyTrigger();
_triggerDaily.Enabled = true;
_triggerDaily.Repetition.Interval = TimeSpan.FromMinutes(2);
_triggerDaily.Repetition.Duration = TimeSpan.FromDays(0);
td.Triggers.Add(_triggerDaily);
// Create an action that will launch ControlHorasGFes.exe whenever the trigger fires
td.Actions.Add(new ExecAction("C:\\Temp\\GFes\\ControlHorasGFes.exe"));
const string taskName = "Launch Task GFes";
// Register the task in the root folder
ts.RootFolder.RegisterTaskDefinition(taskName, td);
ts.FindTask(taskName).Run();
}