Windows Server 2008R2 No Tasks Being Listed

Mar 23, 2011 at 5:37 PM

So I had the service set up to enumerate the tasks and add some info to a datatable, which I then databind to a webpage.  When testing on my local pc, running XP, the service worked perfectly, but as soon as I moved it over to the server, the list of services was null.  I've checked manually using the console, and there is an extensive list of services.  Please advise.
Code is as follows:

 using (TaskService ts = new TaskService( ))
        {

            Version ver = ts.HighestSupportedVersion;
            bool newVer = (ver >= new Version(1, 2));

            // Output all the tasks in the root folder with their triggers and actions
            TaskFolder tf = ts.RootFolder;
            
            foreach (Task t in tf.Tasks)
            {
                tasks.Add(t);
                string trStr = "";
                DataRow r = schedTable.NewRow();
                try
                {
                    r["Name"] = t.Name;

                    foreach (Trigger tr in t.Definition.Triggers)
                    {
                        if (trStr != "")
                            trStr += ";";
                        trStr += tr.ToString();
                    }
                    r["Schedule"] = trStr;
                    r["Next"] = t.NextRunTime;
                    r["Last"] = t.LastRunTime;
                    r["Status"] = t.State;
                    r["Enabled"] = t.Enabled;
                    r["Description"] = t.Definition.RegistrationInfo.Description;

                    schedTable.Rows.Add(r);
                }
                catch { }
            }
        }
 

Coordinator
Mar 23, 2011 at 7:11 PM
Edited Mar 23, 2011 at 7:12 PM

See the bottom of post http://taskscheduler.codeplex.com/discussions/247469 about permissions needed to access the Task Scheduler. Your code is correct, and I know others are having no problems with WS08R2, so I'm guessing it's a permissions issue.

Mar 23, 2011 at 8:02 PM

I had just finished checking that post, and I agree that the situation seems very similar.

Mar 24, 2011 at 6:19 PM

I run this from an administrator account, and I've checked, and I have SeBatchLogonRight permissions, but I still get an access denied error

Coordinator
Mar 24, 2011 at 10:10 PM

Below is a short PowerShell script that should list all the task names in the root folder. Running this while logged in to a user session may show you the errors your C# code may not and should give you some further information about what works and what doesn't. You will need to change the path in the first line to the path on your system. If scripting is restricted, you can open the PowerShell console and cut and paste each line of the script in order for the same results.

Add-Type -Path 'C:\Users\dahall\Documents\Visual Studio 2010\Projects\TaskService\bin\Release\Microsoft.Win32.TaskScheduler.dll'
$ts = New-Object Microsoft.Win32.TaskScheduler.TaskService
$ts.RootFolder.Tasks | ForEach-Object {Write-Host $_.Name}
Mar 31, 2011 at 4:41 PM

The list of tasks is no longer a problem, it was a permissions error.

Adding tasks, however, is still something of a problem.  At any attempt to do so, I am met with "System.Runtime.InteropServices.COMException: (48,7):Body:" as my stack trace at 

ts.RootFolder.RegisterTaskDefinition(txtName.Text.Trim(), td);

My code for this section is:
 protected void btnSave_Click(object sender, EventArgs e)
    {
        TaskDefinition task = (TaskDefinition)Session["Duplicate"];

        

        using (TaskService ts = new TaskService())
        {
            TaskDefinition td = ts.NewTask();

            foreach (Trigger t in task.Triggers)
            {
                td.Triggers.Add((Trigger)t.Clone());
            }
            foreach (Microsoft.Win32.TaskScheduler.Action a in task.Actions)
                td.Actions.Add(a);

            td.RegistrationInfo.Description = task.RegistrationInfo.Description;

            ts.RootFolder.RegisterTaskDefinition(txtName.Text.Trim(), td);
        }

        Session["Duplicate"] = null;

        Response.Redirect("~/Default.aspx");
    }
 

Coordinator
Mar 31, 2011 at 8:52 PM

COMException usually will include the COM error (result code) from the method execution. Can you provide the internal details of the COMException? This could also be a permissions problem as creating task requires more rights than reading tasks. One thing to consider is that the way you are calling it, without any user information, will assume the current user and an interactive logon type.

Coordinator
Apr 24, 2011 at 6:53 AM

I would try the following code which uses cloned Actions as well as cloned triggers, which works for me:

TaskDefinition td = ts.NewTask();
foreach (Trigger t in task.Triggers)
   td.Triggers.Add((Trigger)t.Clone());
foreach (Microsoft.Win32.TaskScheduler.Action a in task.Actions)
   td.Actions.Add((Microsoft.Win32.TaskScheduler.Action)a.Clone());
ts.RootFolder.RegisterTaskDefinition(taskName, td);