Task Scheduler on Windows Server 2012/IIS 8

Topics: Errors
Feb 15, 2016 at 5:28 PM
Hi. Fair warning - I'm not the administrator making changes to the server. I don't understand windows server permissions.

We're trying to upgrade from IIS 7.5 on 2008 to IIS 8 on 2012. The Web Application is attempting to use the Task Scheduler Managed Wrapper to schedule a powershell script (which in turn performs a request to the web application...)

This was all working fine on IIS 7.5/2008 - we had to create a service account and give it permissions to use the task scheduler. Then configure the app pool in IIS which runs the web application to use that service account.

Now that we've upgraded to IIS 8/2012, it doesn't work. Here's what I'm trying to do:

'''
        using (TaskService ts = new TaskService()) {
            Microsoft.Win32.TaskScheduler.Task task = null;
            TaskDefinition td;

            // first make sure there's a task folder
            try {
                ts.RootFolder.CreateFolder("XXX");
            }
            catch (Exception error) {
                sm_log.Info("Error creating XXX task folder in windows scheduler, should be ok: " + error.Message);
            }

            // next, see if the task already exists for this schedule.
            try {
                task = ts.RootFolder.SubFolders["XXX"].Tasks["XXX SCHEDULER " + schedule.id.ToString()];
            }
            catch (Exception error) {
                // this is fine, if task doesn't exist, it'll raise exception
                sm_log.Info("Attempt to edit a schedule " + schedule.id.ToString() + " which doesn't exist; should be fine: " + error.ToString());
            }

            // if the task doesn't exist, create a new one
            if (task == null) {
                td = ts.NewTask();
                td.RegistrationInfo.Description = "XXX Scheduler Task; Test Suite = " + suite.Name + " on Setup " + setup.Name
                                                + " (Suite = " + suite.id + "; setup = " + setup.id + ")";
                string strArguments = @"""" + HostingEnvironment.ApplicationPhysicalPath + @"\bin\Infrastructure\Scheduler.ps1"""
                                    + " -virtualPath " + HostingEnvironment.ApplicationVirtualPath
                                    + " -server " + request.Url.Host
                                    + " -port " + request.Url.Port
                                    + " -projectId " + schedule.ProjectId.ToString()
                                    + " -scheduleId " + schedule.id.ToString();
                // on test and prod, use https
                if (request.IsSecureConnection) {
                    strArguments += " -https";
                }
                td.Actions.Add(new ExecAction("powershell", strArguments));
            }
            else {
                // i.e., task already exists
                td = task.Definition;
            }

            td.Principal.LogonType = TaskLogonType.ServiceAccount;
            td.Principal.RunLevel = TaskRunLevel.Highest;

            // set the time the one-time trigger should run.
            td.Triggers.Clear();
            td.Triggers.Add(new TimeTrigger(schedule.RecurrenceParams.StartTime));

            ts.RootFolder.SubFolders["XXX"].RegisterTaskDefinition("XXX SCHEDULER " + schedule.id.ToString(), td, TaskCreation.CreateOrUpdate, "SYSTEM", null, TaskLogonType.ServiceAccount);
            sm_log.Info("Scheduled task in XXX subfolder: XXX SCHEDULER " + schedule.id.ToString() + " for " + schedule.RecurrenceParams.StartTime);
        }
'''

I'm using the 2.5.4 Task Scheduler. It's an MVC 4 application using 4.5 of dot net. I'd love to think I'm doing something dumb/wrong, e.g.:

'''
            td.Principal.LogonType = TaskLogonType.ServiceAccount;
            td.Principal.RunLevel = TaskRunLevel.Highest;
'''

The error we're getting:

'''
The request is not supported. (Exception from HRESULT: 0x80070032)
at Microsoft.Win32.TaskScheduler.V2Interop.ITaskFolder.GetFolders(Int32 flags)
at Microsoft.Win32.TaskScheduler.TaskFolder.get_SubFolders()
at OurStuff.Scheduler.UpdateSchedule(Schedule schedule, HttpRequestBase request) in OurCode.cs: line 432
at OurStuff.Controller.UpdateSchedule in OurCode.cs: line 137
'''

Thanks!

-Matt LeMay
Coordinator
Feb 16, 2016 at 12:20 AM
I don't have an easy answer for you so let me ask some questions to see if I can't see something. So it appears you're checking for the existence of the folder XXX. Are you able to create it when it doesn't exist? If you remove the subfolder, are you able to create the task in the root folder?