How to create Task into Task Scheduler?

Topics: Errors
Nov 6, 2013 at 5:12 AM
sample code:
        using (TaskService ts = new TaskService())
        {
            // Create a new task definition and assign properties
            using (TaskDefinition td = ts.NewTask())
            {
                td.RegistrationInfo.Description = "Periodically execute HandlerLauncher.ps1 every 2 minutes.";
                // Create a trigger that will fire the task at this time every other day
                using (DailyTrigger dt = new DailyTrigger { DaysInterval = 1 })
                {
                    dt.Repetition.Duration = TimeSpan.FromDays(0);
                    dt.Repetition.Interval = TimeSpan.FromMinutes(2);
                    td.Triggers.Add(dt);

                    string command = "";
                    command = "-nologo -windowstyle hidden -command \"& '" + POWERSHELL_PATH + "\\HandlerLauncher.ps1' -path '" + POWERSHELL_PATH + "'\"";
                    // Create an action that will launch Notepad whenever the trigger fires
                    td.Actions.Add(new ExecAction("%windir%\\system32\\windowspowershell\\v1.0\\powershell.exe", command, null));
                    //td.Actions.Add(new ExecAction("C:\\Program Files (x86)\\Acer\\Acer SCIP\\runps.exe", null, null));

                    // Register the task in the root folder
                    td.Principal.UserId = String.Concat(Environment.UserDomainName, "\\", "Administrator");

                    try
                    {
                        ts.RootFolder.RegisterTaskDefinition(@"TimedPSActivator", td, TaskCreation.Create, td.Principal.UserId, "password", TaskLogonType.Password);
                    }
                    catch (Exception e)
                    {
                        MessageBox.Show("task scheduler exception: " + e);
                    }
                }
            }
        }
My requirement:
   1. I must to assign domain name/ domain administrator, and password to RegisterTaskDefiniation, but still occur problem.
   2. Run whether the user is logged on or not
How could I modified which code? please help me to fix, thanks.
Coordinator
Nov 6, 2013 at 5:24 PM
What error are you getting and what line of code is failing?
Nov 12, 2013 at 7:06 AM
First, thanks to respond quickly by Dahail.

Because we have to use "Run whether user is logged on or not" mode,
I found that it must to modified TaskRunLevel and pass RP.
http://technet.microsoft.com/en-us/library/cc722152.aspx
Nov 19, 2013 at 12:09 AM
iversonjimmy,
Thanks for posting. I have same requirement run task whether the user is logged on or not
Where should I change the code to get work? I have tried following, but not work. what is RP, how to pass it? thank you in advance!!!
Fanyin
                TaskDefinition td = ts.NewTask();
                td.RegistrationInfo.Description = "Import Blood Loss Data";
                td.RegistrationInfo.Author = domain + @"\" + username;  
                td.Principal.RunLevel = TaskRunLevel.Highest;
                td.Principal.LogonType = TaskLogonType.ServiceAccount;
                td.Principal.UserId = domain + @"\" + username;
Nov 19, 2013 at 12:33 AM
It is working now with following code
ts.RootFolder.RegisterTaskDefinition(@"ImportBloodLossData", td, TaskCreation.Create, td.Principal.UserId, password, td.Principal.LogonType)

this works when whether the user is logged on or not
Thanks all!
Sep 4, 2014 at 7:45 AM
+1 Live example. From all the good answers provided by everyone here I am now getting a better understanding of appropriate use of this topic and IMHO other programmers will definitely benifit from the knowledge sharing here. Some of the implementations that I had worked in the past could have been better implemented using this knowledge.


Any final solution with full source code sample application ?

IMHO, better samples for minimize learning curve are real applications with full source code and good patterns

I want execute script powershell, using Tsk scheduler and "Run with highest privileges" option
Coordinator
Sep 4, 2014 at 8:04 PM
kiquenet: Please see the various Documentation pages for sample code, documentation and even a sample project. You can also check out the Source code area and look at the TestTaskService project code for a lot of use patterns. Let me know if you find specific deficiencies I haven't addressed.