0

I have a C# app that will create recurring tasks in the Task Scheduler program to execute a particular program (triggered emails sent based on the recurrence criteria).

Using C# I am able to successfully create the task while running the app in Visual Studio and IIS Express. I see the command window pop up and the task is successfully entered.

However, when entering the program directly through IIS (via a web browser), I see no change in task scheduler and no command window displayed with any status information.

I have tried to run as Administrator to no avail. Based on the code below, what modifications do I need to make to successfully add a scheduled task when one does not exist or modify an existing recurring scheduled task that already exists and just change the days of recurrence?

Here is what I have done so far:

        if (chkActivate.SelectedItem.Text == "Deactivate Reminders")
        {
            chkDaysOfWeek.Enabled = false;
            chkEmailOption.Enabled = false;

            command = "schtasks.exe /Change /TN \"Action Item Reminder\" /Disable";

        }
        else
        {
            chkDaysOfWeek.Enabled = true;
            chkEmailOption.Enabled = true;

            command = "schtasks.exe /Change /TN \"Action Item Reminder\" /Enable";

        }
    List<string> days = new List<string>();

    for (int idx = 0; idx < chkDaysOfWeek.Items.Count; idx++)
    {
        if (chkDaysOfWeek.Items[idx].Selected == true)
        {
            strquery = "UPDATE EmailOption set [Value] = 'true' where [Option] = '" + chkDaysOfWeek.Items[idx].Text.ToString() + "'";
            days.Add(chkDaysOfWeek.Items[idx].Text.Substring(0, 3).ToUpper());
        }
        else
            strquery = "UPDATE EmailOption set [Value] = 'false' where [Option] = '" + chkDaysOfWeek.Items[idx].Text.ToString() + "'";
        SqlCommand cmd = new SqlCommand(strquery, mycon);
        cmd.ExecuteNonQuery();
    }

   // string runascmd = @"schtasks.exe /DELETE /TN ""Action Item Reminder"" /f & schtasks.exe /CREATE /SC WEEKLY /D " + string.Join(",", days) + @" /TN ""Action Item Reminder"" /TR ""C:\ActionAIM_Source\bin\ActionItemReminder.exe"" /ST 00:01 /f & """ + command;
    string runascmd = @"schtasks.exe /CREATE /SC WEEKLY /D " + string.Join(",", days) + @" /TN ""Action Item Reminder"" /TR ""C:\ActionAIM_Source\bin\ActionItemReminder.exe"" /ST 00:01 /f & " + command;

    ProcessStartInfo processInfo = new ProcessStartInfo("cmd.exe");
    processInfo.Arguments = @"runas /env /user:Administrator /K """ + runascmd + @"""";
    //processInfo.Verb = "runas";
    processInfo.UseShellExecute = true;

    try
    {
        Process.Start(processInfo);
    }
    catch (Win32Exception ex)
    {
        Response.Write(ex.ToString());   
    }
Vahe
  • 1,699
  • 3
  • 25
  • 76
  • have you tried to set administrative credentials on the app pool running your web app? – mrtig Aug 22 '14 at 02:13
  • I have tried altering the app pool to LocalSystem (current setting for now), and my own user account for Windows. None of them made a change. So probably the code is not the issue then. – Vahe Aug 22 '14 at 02:25
  • are you able to step through the code and see what happens? Also, have you looked at this? http://stackoverflow.com/a/4679686/2638872 – mrtig Aug 22 '14 at 02:29
  • Thank you for the link. I have tried the primary suggestion of enabling IIS Admin Service to interact with desktop but was not able to produce a solution. I have observed my program working when I only "Change" (meaning, Enable or Disable) the scheduled task. When I try to "Create" or "Delete" the program does nothing or hangs. Is this still a permission issue? – Vahe Aug 22 '14 at 05:03

1 Answers1

0

After modifying the code to have the command ProcessStartInfo("schtasks.exe") instead of ProcessStartInfo("cmd.exe"), I then split the command line string into two commands and set the App Pool to run as my local account for Windows, I was able to create the task and modify the task I created successfully via IIS.

        if (chkActivate.SelectedItem.Text == "Deactivate Reminders")
        {
            chkDaysOfWeek.Enabled = false;
            chkEmailOption.Enabled = false;

            command = @"/Change /TN ""Action Item Reminder"" /Disable";

        }
        else
        {
            chkDaysOfWeek.Enabled = true;
            chkEmailOption.Enabled = true;

            command = @"/Change /TN ""Action Item Reminder"" /Enable";

        }
        if (chkEmailOption.SelectedItem.Text == "Original")
        {
            strquery = "UPDATE EmailOption set [Value] = 'true' where [Option] = 'Original Due Date'; UPDATE EmailOption set [Value] = 'false' where [Option] = 'ECD'";
            SqlCommand cmd = new SqlCommand(strquery, mycon);
            cmd.ExecuteNonQuery();
        }
        else if (chkEmailOption.SelectedItem.Text == "ECD")
        {
            strquery = "UPDATE EmailOption set [Value] = 'true' where [Option] = 'ECD'; UPDATE EmailOption set [Value] = 'false' where [Option] = 'Original Due Date'";
            SqlCommand cmd = new SqlCommand(strquery, mycon);
            cmd.ExecuteNonQuery();
        }


        List<string> days = new List<string>();

        for (int idx = 0; idx < chkDaysOfWeek.Items.Count; idx++)
        {
            if (chkDaysOfWeek.Items[idx].Selected == true)
            {
                strquery = "UPDATE EmailOption set [Value] = 'true' where [Option] = '" + chkDaysOfWeek.Items[idx].Text.ToString() + "'";
                days.Add(chkDaysOfWeek.Items[idx].Text.Substring(0, 3).ToUpper());
            }
            else
                strquery = "UPDATE EmailOption set [Value] = 'false' where [Option] = '" + chkDaysOfWeek.Items[idx].Text.ToString() + "'";
            SqlCommand cmd = new SqlCommand(strquery, mycon);
            cmd.ExecuteNonQuery();
        }

        string create = @"/CREATE /SC WEEKLY /D " + string.Join(",", days) + @" /TN ""Action Item Reminder"" /TR ""C:\ActionAIM_Source\bin\ActionItemReminder.exe"" /ST 00:01 /f";
        ProcessStartInfo processInfo = new ProcessStartInfo("schtasks.exe");

        try
        {
            processInfo.Arguments = create;
            var process = Process.Start(processInfo);
            process.WaitForExit(1000);
            processInfo.Arguments = command;
            process = Process.Start(processInfo);
            process.WaitForExit(1000);
        }
       catch (Win32Exception ex)
       {
            Response.Write(ex.ToString());   
       }
Vahe
  • 1,699
  • 3
  • 25
  • 76