This project has moved. For the latest updates, please go here.

XP with System user, Interactive flag?

Aug 4, 2010 at 11:52 PM
Edited Aug 4, 2010 at 11:53 PM

I'm trying to set up an interactive task with XP's v1.0 Task Scheduler.

With this existing code below (I did not include the references to all the variables), the task only runs in the background (seen in the task manager). I managed to get that far looking at some of the older discussions posted here.

If I use the AT command with the /interactive flag, the task executes and runs interactively.  What would be the equivalent of that in the Task Scheduler library using C#?

 

 

                                        TaskService ts = new TaskService();
                                        TaskDefinition td = ts.NewTask();
                                        td.Actions.Add(new ExecAction(deployPath + deployAppName, "", null));
                                        td.Settings.ExecutionTimeLimit = TimeSpan.FromHours(25);
                                        td.Principal.LogonType = TaskLogonType.InteractiveToken;
                                        td.Principal.UserId = @"NT AUTHORITY\System";

                                        Common.DaysOfTheWeek week = new Common.DaysOfTheWeek();

                                        foreach (string day in week)
                                        {
                                            if (days_times.get(day) != string.Empty)
                                            {
                                                DateTime dt = Common.formatTimeForLoading(days_times.get(day));
                                                dt = dt.AddMinutes(Convert.ToDouble(offset_delay));

                                                Microsoft.Win32.TaskScheduler.DaysOfTheWeek theday = (Microsoft.Win32.TaskScheduler.DaysOfTheWeek)Enum.Parse(typeof(Microsoft.Win32.TaskScheduler.DaysOfTheWeek), char.ToUpper(day[0]) + day.Substring(1));
                                                td.Triggers.Add(new Microsoft.Win32.TaskScheduler.WeeklyTrigger { StartBoundary = dt, DaysOfWeek = theday });
                                            }
                                        }

                                        ts.RootFolder.RegisterTaskDefinition(scheduledTaskName, td, TaskCreation.Create, null, null, TaskLogonType.ServiceAccount,null);
                                        

Any suggestions?  Thanks :)

 

Coordinator
Aug 5, 2010 at 3:58 PM
I haven't tested this, but believe it is correct. Task registration will override any previously set Principal information. Omit the lines
td.Principal.LogonType = TaskLogonType.InteractiveToken;
td.Principal.UserId = @"NT AUTHORITY\System";

Add a line

td.Settings.RunOnlyIfLoggedOn = true;

And change the last line to

ts.RootFolder.RegisterTaskDefinition(scheduledTaskName, td, TaskCreation.Create, @"NT AUTHORITY\System", null, TaskLogonType.InteractiveToken, null);
Aug 5, 2010 at 7:24 PM

I tried your suggestions, but the task doesn't appear to run at all.

 

 

 TaskService ts = new TaskService();
                                        TaskDefinition td = ts.NewTask();
                                        td.Actions.Add(new ExecAction(deployPath + deployAppName, "", null));
                                        td.Settings.ExecutionTimeLimit = TimeSpan.FromHours(25);
                                        td.Settings.RunOnlyIfLoggedOn = true;

                                        Common.DaysOfTheWeek week = new Common.DaysOfTheWeek();

                                        foreach (string day in week)
                                        {
                                            if (days_times.get(day) != string.Empty)
                                            {
                                                DateTime dt = Common.formatTimeForLoading(days_times.get(day));
                                                dt = dt.AddMinutes(Convert.ToDouble(offset_delay));

                                                Microsoft.Win32.TaskScheduler.DaysOfTheWeek theday = (Microsoft.Win32.TaskScheduler.DaysOfTheWeek)Enum.Parse(typeof(Microsoft.Win32.TaskScheduler.DaysOfTheWeek), char.ToUpper(day[0]) + day.Substring(1));
                                                td.Triggers.Add(new Microsoft.Win32.TaskScheduler.WeeklyTrigger { StartBoundary = dt, DaysOfWeek = theday });
                                            }
                                        }

                                        ts.RootFolder.RegisterTaskDefinition(scheduledTaskName, td, TaskCreation.Create, @"NT AUTHORITY\System", null, TaskLogonType.InteractiveToken, null);

 

 

Coordinator
Aug 6, 2010 at 2:31 AM

I just ran the code on my XP VM. Specify "SYSTEM" for the user name and it does run. However, it will not run in the user session of the logged on user. It will run under SYSTEM's user session. You can see the task in the scheduled tasks applet and see the process start under the SYSTEM account. If the task needs to run in the user session, it has to be registered to run as the user.

Aug 6, 2010 at 7:35 PM

hmm...

In XP, is there a way to schedule something to run as *any* user who logs in?

Under Windows 7, I got it to work by specifying the user as "MyDomain\Domain Users".  Under XP, it doesn't seem to allow that.

Coordinator
Aug 8, 2010 at 4:55 AM

See this thread for your answer:

http://taskscheduler.codeplex.com/Thread/View.aspx?ThreadId=45013

Aug 9, 2010 at 8:12 PM

That code doesn't work.  It's the same thing that I included in my first post.

Aug 11, 2010 at 12:00 AM

I get an error thrown when I try using the recommended code on XP:

 

TaskService ts = new TaskService();
TaskDefinition td = ts.NewTask();
td.Actions.Add(new ExecAction(deployPath + deployAppName, "", null));
td.Settings.ExecutionTimeLimit = TimeSpan.FromHours(25);
td.Settings.RunOnlyIfLoggedOn = true;
td.Principal.Id = @"NT AUTHORITY\System";
td.Principal.LogonType = TaskLogonType.InteractiveToken;

Common.DaysOfTheWeek week = new Common.DaysOfTheWeek();

foreach (string day in week)
{
    if (days_times.get(day) != string.Empty)
    {
        DateTime dt = Common.formatTimeForLoading(days_times.get(day));
        dt = dt.AddMinutes(Convert.ToDouble(offset_delay));

        Microsoft.Win32.TaskScheduler.DaysOfTheWeek theday = (Microsoft.Win32.TaskScheduler.DaysOfTheWeek)Enum.Parse(typeof(Microsoft.Win32.TaskScheduler.DaysOfTheWeek), char.ToUpper(day[0]) + day.Substring(1));
        td.Triggers.Add(new Microsoft.Win32.TaskScheduler.WeeklyTrigger { StartBoundary = dt, DaysOfWeek = theday });
    }
}

ts.RootFolder.RegisterTaskDefinition(scheduledTaskName, td, TaskCreation.Create, null, null, TaskLogonType.ServiceAccount, null);

 

 

NoV1Supported..ctor is not supported on Task Scheduler 1.0

at Microsoft.Win32.TaskScheduler.TaskPrincipal.set_Id(String value)

 

Coordinator
Aug 17, 2010 at 8:00 PM

This is working as it should. The TaskPrincipal.Id property is V2 only. You cannot set it under XP. Remove that line or make it conditional on

if (ts.HighestSupportedVersion >= new Version(1, 2))
   ...