Error on task creation, about LoginType

Aug 12, 2009 at 9:37 AM

Hi all,

I'm trying to create a new task using this code:

                    ExecAction action = new ExecAction("HTTPRequest.vbs", "http://" + Request.Url.Host + "/scheduler/" + taskPage, "C:\\inetpub\\ftproot\\LocalUser\\MyAccount\\scheduler");
                    TaskDefinition definition = service.NewTask();

                    definition.RegistrationInfo.Description = taskName;
                    
                    definition.Actions.Add(action);
                    
                    definition.Settings.AllowHardTerminate = false;
                    
                    definition.Principal.LogonType = TaskLogonType.ServiceAccount;
                    definition.Principal.UserId = String.Empty;

                    TimeTrigger trigger = new TimeTrigger();

                    definition.Triggers.Add(trigger);

                    trigger.StartBoundary = DateTime.Now.AddMinutes(2);
                    trigger.EndBoundary = trigger.StartBoundary.AddHours(12);
                    
                    service.GetFolder(\\MyFolder).RegisterTaskDefinition("Task " + e.CommandArgument + " - User " + Session["UserID"], definition);

Now, when I try to run this code I get only:
 
"(41,4):LogonType:"
as exception message. Fired here:
Microsoft.Win32.TaskScheduler.V2Interop.ITaskFolder.RegisterTaskDefinition(String Path, ITaskDefinition pDefinition, Int32 flags, Object UserId, Object password, TaskLogonType LogonType, Object sddl)
   at Microsoft.Win32.TaskScheduler.TaskFolder.RegisterTaskDefinition(String Path, TaskDefinition definition, TaskCreation createType, String UserId, String password, TaskLogonType LogonType, String sddl)
   at Microsoft.Win32.TaskScheduler.TaskFolder.RegisterTaskDefinition(String Path, TaskDefinition definition)
   at
 
I have looked here for similar troubles, without luck. Please, has anyone had the same problem before?
 
Thanks in advance.
 
Aug 12, 2009 at 12:29 PM

I forgot to give you some essential details:

OS: Windows Server 2008

Application type: ASP.NET 2.0

Hoping someone can help me.

Coordinator
Aug 13, 2009 at 9:32 PM
Edited Aug 13, 2009 at 9:33 PM

This is the code I ran to make it work. I had to change some of the code because of local variables you had. The only real change is that the Principle.UserId assignment needs to be to "System". I also reorganized the code a bit as you will see.

using (TaskService service = new TaskService())
{
	ExecAction action = new ExecAction("HTTPRequest.vbs", "http://somesite.com", null);

	TimeTrigger trigger = new TimeTrigger();
	trigger.StartBoundary = DateTime.Now.AddMinutes(2);
	trigger.EndBoundary = trigger.StartBoundary.AddHours(12);

	TaskDefinition definition = service.NewTask();
	definition.RegistrationInfo.Description = "Desc";
	definition.Actions.Add(action);
	definition.Settings.AllowHardTerminate = false;
	definition.Principal.LogonType = TaskLogonType.ServiceAccount;
	definition.Principal.UserId = "System";
	definition.Triggers.Add(trigger);
    
	service.RootFolder.RegisterTaskDefinition("Task - User", definition);
}
Aug 14, 2009 at 9:26 AM

Thank you dahall! It's working now.

By the way, congratulations for your job.. It's a very very well done project.

Hope to hear from you and your project soon.

May 25, 2010 at 1:57 AM

I am trying to get this working on Vista ( though using Visual Basic) and making almost exactly the
same call as described above.

I am impersonating an administrator who has access to the task scheduler (can create tasks using the Task Scheduler windows app)

I am using the DLL as a reference in ASP.NET and making the calls from a web page. The web page has annoymous access
turned off and I am successfully impersonating the underlying windows user - who is an administrator.

However I always get an access denied error  - Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))

The only way I can avoid this is to specify the user name and the password of the administrator in the RegisterTaskDefintion call.

Any ideas /pointers would be very welcome.

 

 

 

Coordinator
Jun 15, 2010 at 6:43 PM

I would try registering the task as either "System" or with the username and the login type of TaskLogonType.S4U as follows:

ts.RootFolder.RegisterTaskDefinition(taskName, td, TaskCreation.Create, @"DOMAIN\user", null, TaskLogonType.S4U, null);