简体   繁体   中英

Creating a generic Task scheduler for all users using C#

I need a solution in task scheduler, were in created task should be running for all the users logging in to the workstation irrelevant to how has created it.

I wrote a console app using C# to create a task in task scheduler.

Say, in positive scenario
When user runs the console app its creates a task and task principle user id will be his username. So user can run that task because he is the owner.

Say, in negative scenario
When user runs the console app as "run as administrator" with different credential say Myadmin then task is created but with priniciple user id as admin's id not the one logged in user.

Now comes the problem. After closing everything and he tries to run the task it will not run because owner/principle id is different.

I need a solution as- task will be created by admin and all the users logging into that machine should be getting the notification irrelevant to who has created/ owner.

Here is my code.

    using Microsoft.Win32.TaskScheduler;

    static readonly string taskName = "Hello World";
    static void Main(string[] args)
    {

        try
        {
            Console.WriteLine("Creating scheduler");
            using (TaskService ts = new TaskService())
            {
                TaskDefinition td = ts.NewTask();
                td.Data = "Hello World Notification";                     
                td.Principal.LogonType = TaskLogonType.InteractiveToken;

                td.RegistrationInfo.Description = "Hello World";
                td.RegistrationInfo.Documentation = "Auto notification Hello World";
                td.Settings.DisallowStartIfOnBatteries = false;

                td.Settings.Priority = System.Diagnostics.ProcessPriorityClass.Normal;

                td.Settings.RunOnlyIfNetworkAvailable = true;
                td.Settings.StopIfGoingOnBatteries = false;                
                td.Settings.MultipleInstances = TaskInstancesPolicy.StopExisting;
                td.Settings.StartWhenAvailable = true;

                td.Triggers.Add(new DailyTrigger(1));

                td.Triggers[0].StartBoundary = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 12, 0, 1);

                td.Actions.Add(new ExecAction(Environment.CurrentDirectory + "\\Earth.exe", ""));

                // Register the task in the root folder

                ts.RootFolder.RegisterTaskDefinition(taskName, td);

            }
            Console.WriteLine("Success");

        }
        catch (Exception ex)
        {
            Console.WriteLine("Error while creating schedular");
            Console.WriteLine(ex.Message);
            Console.WriteLine("**********************");
            Console.WriteLine("PRESS ANY KEY TO EXIT");
            Console.ReadLine();
        }    

Pass the correct user's credentials in when you use RegisterTaskDefinition

ts.RootFolder.RegisterTaskDefinition(taskName, td, TaskCreation.CreateOrUpdate, "UserId", "Password", TaskLogonType.Password);

You can probably remove this line too

td.Principal.LogonType = TaskLogonType.InteractiveToken;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM