How to specify Username/Password with Task

Feb 2, 2011 at 8:44 PM

Using the Notepad example on the Codeplex web site, I am trying to figure out how to create a new task and specify the Windows Domain, Username, and Password to be used when the Task is Executed (ie: The value defined in the 'Run As' box of a Task's Properties. I am writing my code in Microsoft Visual Basic .NET 2010.

Below is my code:

Dim ts As New TaskService

Dim td As TaskDefinition

td = ts.NewTask

td.RegistrationInfoDescription = "Test"

td.Triggers.Add(New DailyTrigger(daysInterval:=2))

td.Actions.Add(New ExeAction("notepad.exe", "c:\test.txt"))

ts.RootFolder.RegisterTaskDefinition("Test", td, TaskCreation.Create, "domain\userid", "password")

If I leave off the last three parameters of the 'RegiserTaskDefinition' call, it creates the task and defaults the 'Run As' Property of the task as 'NT AUTHORITY\SYSTEM', but this task will not run unless I change the 'Run As' property value to a valid Windows domain and userid, and then click on the Password Button to define the Password.

If I add the last three parameters, I get a message that this is not supported in the 1.0 version of the task scheduler. I am using the 1.0 version, because my laptop is running on Windows XP, so my question would be: Is there a way to specify the domain\userid and password when creating a task (programatically), with the 1.0 version of the Microsoft Task Scheduler installed?

Feb 3, 2011 at 1:16 AM

You almost got it. You only need to add the appropriate TaskLogonType value as the last parameter:

ts.RootFolder.RegisterTaskDefinition("Test", td, TaskCreation.Create, "domain\userid", "password", TaskLogonType.Password)
Jun 30, 2011 at 2:35 PM

I am using the method mentioned in this thread to create a task for a user + password and it is working fine.  The problem I have is when the user does not have a logon password.  If you pass the user and a blank password, Windows gives an access denied error.

How do I use it when the user has no password?

Jun 30, 2011 at 9:55 PM

Try passing in String.Empty or "".

Jun 30, 2011 at 11:01 PM

When I said blank password I meant that I am passing an empty string.  After reading in other places I am comming to the conclusion that Windows will not allow a task to be scheduled if the user does not have a password.  Advice I am reading elsewhere suggests that you have to give the user account a password.  Can anyone else confirm this?

Jul 2, 2011 at 8:20 PM

Does the same error occur when you pass 'null' for the password?