Log On trigger for logon of any user

Feb 20, 2012 at 9:58 AM
Edited Feb 20, 2012 at 9:59 AM


I am facing a problem while creating a task which is triggered on log on of any user but which uses the credentials of a specific user. I am getting a Access Denied Exception while trying to do so.

After reading the comments for the GroupId property which is as follows:

If you want a task to be triggered when any member of a group logs on to the computer rather than when a specific user logs on, then do not assign a value to the LogonTrigger.UserId property. Instead, create a logon trigger with an empty LogonTrigger.UserId property and assign a value to the principal for the task using the Principal.GroupId property.

i tried setting the Group id as well but was left with the same result.  Please help me to create a task with this requirement.

The code that i am using currently is as follows:

private void ConfigureTaskDefinition(Schedule schedule, TaskDefinition taskDefinition)
            if (schedule.RunEvenIfNotLoggedOn)
                taskDefinition.Principal.LogonType = TaskLogonType.InteractiveTokenOrPassword;
                taskDefinition.Principal.LogonType = TaskLogonType.InteractiveToken;

            if (!_isV2TaskScheduler.Value && !schedule.RunEvenIfNotLoggedOn)
                taskDefinition.Settings.RunOnlyIfLoggedOn = true;

            taskDefinition.Principal.UserId ="MyDomain\\User1";

            taskDefinition.RegistrationInfo.Author = GetTaskAuthorName(schedule.ProductLongName);

            taskDefinition.RegistrationInfo.Description = string.IsNullOrEmpty(schedule.Description)
                                                              ? string.Empty
                                                              : schedule.Description;

            taskDefinition.Settings.DisallowStartIfOnBatteries = schedule.RunIfComputerIsOnACPower;

            taskDefinition.Settings.Enabled = schedule.Enable;

          using (var win32Trigger = new Microsoft.Win32.TaskScheduler.LogOnTrigger{UserId="MyDomain\\User2"})
                    taskDefinition.Principal.GroupId = "Users";

            AddAction(schedule, taskDefinition);
Note: Here 'Schedule' is a class which we use to store information retrieved from the user.