I need to create windows Tasks programmatically using ASP.net and C# (Windows Server 2003). When I use following code:
Process p = new Process();
p.StartInfo.FileName = "schtasks.exe";
p.StartInfo.Arguments = " /create /tn MyTask /tr notepad.exe /sc DAILY /st 10:00:00 /ru myUsername /rp myPassword ";
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.Start();
p.WaitForExit();
It create task successfully, but task don’t execute and says “Could not start”
Also in log file it says: "MyTask.job" (notepad.exe) 10/9/2012 10:00 PM ** ERROR ** The attempt to retrieve account information for the specified task failed; therefore, the task did not run. Either an error occurred, or no account information existed for the task. The specific error is: 0x8004130f: No account information could be found in the Task Scheduler security database for the task indicated.
When I go through Control panel/Scheduled tasks and right click on my task and set my password again, my task will be run successfully in next time. How can I do? Also when I change my code to:
Process p = new Process();
p.StartInfo.FileName = "schtasks.exe";
p.StartInfo.Arguments = " /create /tn MyTask /tr notepad.exe /sc DAILY /st 10:00:00 /s system /u myUsername /p myPassword ";
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.Start();
p.WaitForExit();
It makes this error:
ERROR: Passing the user credential on local connection
I would rather use managed Task Scheduler Wrapper, IMO it's much cleaner approach than staring a another process, even more in the case of ASP.NET application.
But if you still prefer using schtasks.exe see this SO answer :
User contributions licensed under CC BY-SA 3.0