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

Specifying task account information

Aug 20, 2008 at 12:49 PM
In the previous version of Task Scheduler 1.0 wrapper on CodeProject there was a method SetAccountInformation() for Task object.
For now I didn't find any way to specify account AND password for a given task since there is only one SetAccountInformation() call in TaskPrincipal.UserId property.
Assuming we're using Windows XP with TS 1.0 there is no way to run task under defined account.
Sep 1, 2008 at 11:18 AM
Hi, try my updated method TaskFolder.RegisterTaskDefinition(string Path, TaskDefinition definition, TaskCreation createType, string UserId, string password, TaskLogonType LogonType, string sddl) in TaskFolder.cs:

public Task RegisterTaskDefinition(string Path, TaskDefinition definition, TaskCreation createType, string UserId,
string password, TaskLogonType LogonType, string sddl)
{
if (v2Folder != null)
{
return
new Task(v2Folder.RegisterTaskDefinition(Path, definition.v2Def, (int) createType, UserId, password,
LogonType, sddl));
}

TaskFlags flags = definition.v1Task.GetFlags();

IntPtr pwd;
switch (LogonType)
{
case TaskLogonType.Group:
case TaskLogonType.S4U:
case TaskLogonType.None:
throw new NotV1SupportedException("This LogonType is not supported on Task Scheduler 1.0.");
case TaskLogonType.InteractiveToken:
flags |= TaskFlags.RunOnlyIfLoggedOn;
flags |= TaskFlags.Interactive;
if (String.IsNullOrEmpty(UserId)) UserId = WindowsIdentity.GetCurrent().Name;
definition.v1Task.SetAccountInformation(UserId, IntPtr.Zero);
break;
case TaskLogonType.ServiceAccount:
if (String.IsNullOrEmpty(UserId)) definition.v1Task.SetAccountInformation(String.Empty, IntPtr.Zero);
else definition.v1Task.SetAccountInformation(UserId, IntPtr.Zero);
break;
case TaskLogonType.InteractiveTokenOrPassword:
pwd = Marshal.StringToCoTaskMemUni(password);
definition.v1Task.SetAccountInformation(UserId, pwd);
Marshal.FreeCoTaskMem(pwd);
flags |= TaskFlags.Interactive;
break;
case TaskLogonType.Password:
pwd = Marshal.StringToCoTaskMemUni(password);
definition.v1Task.SetAccountInformation(UserId, pwd);
Marshal.FreeCoTaskMem(pwd);
break;
default:
break;
}
definition.v1Task.SetFlags(flags);

switch (createType)
{
case TaskCreation.Create:
case TaskCreation.CreateOrUpdate:
case TaskCreation.Disable:
case TaskCreation.Update:
if (createType == TaskCreation.Disable) definition.Settings.Enabled = false;
definition.V1Save(Path);
break;
case TaskCreation.DontAddPrincipalAce:
throw new NotV1SupportedException("Security settings are not available on Task Scheduler 1.0.");
case TaskCreation.IgnoreRegistrationTriggers:
throw new NotV1SupportedException("Registration triggers are not available on Task Scheduler 1.0.");
case TaskCreation.ValidateOnly:
throw new NotV1SupportedException("Xml validation not available on Task Scheduler 1.0.");
default:
break;
}

return new Task(definition.v1Task);
}
Arcao.
Coordinator
Oct 14, 2008 at 11:52 PM
This has been added to the current release.