I want to register a new task. My app has admin privileges:
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
I have tried to find something on the Internet, but there are so many examples that tell me I'm doing it right.
Here's an exception I'm getting:
COMException was caught Account restrictions are preventing this user from signing in. For example: blank passwords aren't allowed, sign-in times are limited or a policy restriction has been enforced. (Exception from HRESULT: 0x8007052F)
I don't know why it's not working for me...
Here's my method:
private void CreateTask()
{
using (var ts = new TaskService())
{
string computerName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
TaskDefinition td = ts.NewTask();
td.RegistrationInfo.Date = DateTime.Now;
td.RegistrationInfo.Author = computerName;
td.RegistrationInfo.Description = "This task is awesome.";
var trigger = new DailyTrigger(1)
{
Enabled = true,
StartBoundary = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd") + "T05:00:00")
};
td.Triggers.Add(trigger);
td.Principal.Id = "Author";
td.Principal.UserId = computerName;
td.Principal.LogonType = TaskLogonType.Password;
td.Principal.RunLevel = TaskRunLevel.Highest;
td.Settings.MultipleInstances = TaskInstancesPolicy.IgnoreNew;
td.Settings.DisallowStartIfOnBatteries = true;
td.Settings.StopIfGoingOnBatteries = true;
td.Settings.AllowHardTerminate = true;
td.Settings.StartWhenAvailable = false;
td.Settings.RunOnlyIfNetworkAvailable = false;
td.Settings.IdleSettings.StopOnIdleEnd = true;
td.Settings.IdleSettings.RestartOnIdle = false;
td.Settings.AllowDemandStart = true;
td.Settings.Enabled = true;
td.Settings.Hidden = false;
td.Settings.RunOnlyIfIdle = false;
td.Settings.WakeToRun = false;
td.Settings.ExecutionTimeLimit = TimeSpan.FromHours(8);
td.Settings.Priority = (ProcessPriorityClass) 7;
td.Actions.Add(new ExecAction(Path.Combine(Config.MainDir, "Scripts", "starter.bat"),
workingDirectory: Path.Combine(Config.MainDir, "Scripts")));
ts.RootFolder.RegisterTaskDefinition("My Task", td);
}
}
To "Run whether the user is logged on or not", these lines are not needed:
td.Principal.LogonType = TaskLogonType.Password;
ts.RootFolder.RegisterTaskDefinition("My Task", td);
Instead, use this line:
ts.RootFolder.RegisterTaskDefinition(taskName, td, TaskCreation.CreateOrUpdate, "someUserId", "somePassword", TaskLogonType.Password);
I give credit here: https://taskscheduler.codeplex.com/discussions/268726
If you set the Principal.LogonType
to TaskLogonType.Password
you must provide it with a password. So you have 2 options:
td.Principal.LogonType =
TaskLogonType.Password`User contributions licensed under CC BY-SA 3.0