This project has moved. For the latest updates, please go here.

Creating Task using System Account

May 25, 2011 at 7:13 PM

I am attempting to create a scheduled task that will run with the system account.  This is for a 2003 machine.  The account I'm running the program with is using a privileged account.

Code:

Dim ts As TaskService = New TaskService()
ts.TargetServer = "MyServer"
Dim td As TaskDefinition = ts.NewTask()
td.RegistrationInfo.Description = "just a test here"
Dim timeTrig As TimeTrigger = New TimeTrigger(New Date(2011, 8, 20, 5, 4, 3, 2))
timeTrig.EndBoundary = New Date(2011, 8, 20, 6, 5, 4, 3)
td.Triggers.Add(timeTrig)
td.Actions.Add(New ExecAction("cmd.exe"))
ts.RootFolder.RegisterTaskDefinition("winpatches", td, TaskCreation.CreateOrUpdate, Nothing, Nothing, TaskLogonType.ServiceAccount, Nothing)

I get:

The task has been configured with an unsupported combination of account settings and run time options. (Exception from HRESULT: 0x80041314)

 

What am I doing wrong?  How can I create a scheduled task that will run as the system account.

Coordinator
May 26, 2011 at 12:57 AM

Try specifying "SYSTEM" for the userId parameter on RegisterTaskDefinition.

May 26, 2011 at 6:32 PM

I tried this and I get the same result.  I also tried multiple servers to make sure it was specific to one.  

Coordinator
May 27, 2011 at 7:36 AM

Will you post the full exception stack so I can see exactly which line of code is causing the exception within the libary?

May 27, 2011 at 1:14 PM
Edited May 27, 2011 at 1:15 PM

Senario:

Trying to create a scheduled task on Server 2003 R2 from a Server 2008 R2 box that will run as the "NT AUTHORITY\SYSTEM" Account

Revised Code:

Dim ts As TaskService = New TaskService()
ts.TargetServer = "MyServer"
Dim td As TaskDefinition = ts.NewTask()
td.RegistrationInfo.Description = "just a test here"
Dim timeTrig As TimeTrigger = New TimeTrigger(New Date(2011, 8, 20, 5, 4, 3, 2))
timeTrig.EndBoundary = New Date(2011, 8, 20, 6, 5, 4, 3)
td.Triggers.Add(timeTrig)
td.Actions.Add(New ExecAction("cmd.exe"))
ts.RootFolder.RegisterTaskDefinition("testTask", td, TaskCreation.CreateOrUpdate, "SYSTEM", Nothing, TaskLogonType.ServiceAccount, Nothing) <----Exception Occurs here

Full Exception:

System.Runtime.InteropServices.COMException was unhandled
  ErrorCode=-2147216620
  Message=The task has been configured with an unsupported combination of account settings and run time options. (Exception from HRESULT: 0x80041314)
  Source=Microsoft.Win32.TaskScheduler
  StackTrace:
       at 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 playgroundvb.Class1.schedulPatches() in E:\src\Cameron\playgroundvb\playgroundvb\Class1.vb:line 50
       at playgroundvb.Class1.Main() in E:\src\Cameron\playgroundvb\playgroundvb\Class1.vb:line 18
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: Nothing
Coordinator
May 28, 2011 at 6:16 AM

Let me know if changing the first two lines of your supplied code to following corrects the error:

Dim ts As TaskService = New TaskService("MyServer", Nothing, Nothing, Nothing, True)
May 29, 2011 at 8:41 PM

Dim ts As TaskService = New TaskService("MyServer", Nothing, Nothing, Nothing, True)

 works, but if you specified username or password it didnt work!
Dim ts As TaskService = New TaskService("192.168.13.44", "ISR_Task, "main", "PaSSword, True)
Error: Unknow Username or Password
Server: 2008 R2
Username and Password are correctly and the eventviewer is empty.
If i try
Dim ts As TaskService = New TaskService("192.168.13.44", Nothing, Nothing, Nothing, True)
eventviewer has 1 Entry with the username from the localmachine (nothing wrong)
Coordinator
May 30, 2011 at 4:24 AM

The three middle parameters are userName, userDomain & password. Make sure they are in that order.

May 30, 2011 at 7:34 PM

"main" is the name of my computer. i haven't a domain.

Coordinator
May 31, 2011 at 4:52 AM

Those middle parameters are for an account that has permissions on the target computer. You can't use the local machine account as it will have no permissions.

May 31, 2011 at 8:54 PM

Administratoraccount on server named "MAIN" with the IP 192.168.13.44  :

Username: ISR_TASK

Passwort: PaSSword

I try'd to connect from a other PC with

Dim ts As TaskService = New TaskService("192.168.13.44", "ISR_Task, "main", "PaSSword, True)
What's wrong?
Thx for your support!
Coordinator
Jun 2, 2011 at 6:40 AM

I just did a test against a remote non-domain Win7 box and this worked:

Dim ts As TaskService = New TaskService("192.168.13.44", "ISR_Task", Nothing, "PaSSword", True)
Jun 3, 2011 at 3:50 PM

I try this but then come the next error:

Object reference not set to an instance of an object.

and the eventviewer has no entry... I think i give up..

Coordinator
Jun 6, 2011 at 6:09 PM

One last thought. Make sure to enable the File and Printer Sharing firewall exception on the remote computer.