C# – Run a scheduled task from web application

asp.netcnetscheduled-tasks

I am using the following code to execute a Windows Scheduled Task from my web application. Both the web application and the scheduled application runs on the same server.

var proc = new Process
{
    StartInfo =
    {
        UseShellExecute = false,
        FileName = @"C:\Windows\System32\schtasks.exe",
        Arguments = "/run /tn StartDataSync",
        UserName = "admin123",
        Password = passwd
    }
};
proc.Start();
proc.WaitForExit();

Now I would like to run the same scheduled application from a web application running on another machine in the same network domain. When I researched I found like I can specify the name or IP address of the remote computer I want to connect to in the /S system argument. So I tried the following code, but its not working.

var proc = new Process
{
    StartInfo =
    {
        UseShellExecute = false,
        FileName = @"C:\Windows\System32\schtasks.exe",
        Arguments = "/run /S 192.168.5.202 /tn StartDataSync",
        UserName = "admin123",
        Password = passwd
    }
};
proc.Start();
proc.WaitForExit();

Best Answer

Try this answer ..according to this post

using TaskScheduler;

using (TaskService tasksrvc = new TaskService(server.Name, login, domain, password))
{
    Task task = tasksrvc.FindTask(taskName);
    task .Run();       
}

dll for task scheduler can be found here https://github.com/dahall/taskscheduler