When calling "RegisterTaskDefinition", the error I am getting is:
the task xml contains a value which is incorrectly formatted or out of range
Exception from HResult:0x80041318
Any help is appreciated.
Without information regarding the settings, triggers, and actions you have added to the TaskDefinition, I am unable to troubleshoot your issue. Please also ensure that the account running your code has sufficient permissions to create a task. I will also need to know the OS on which you're running the code and how you are instantiating the TaskService instance.
Thanks for looking into the issue. I am getting the error while running the code on Windows 10 professional only. On other windows versions it is working correctly. The code is as following:
public void CreateSchedulerTask()
{
try
{
using (TaskService taskService = new TaskService())
{
bool IsNewer = taskService.HighestSupportedVersion > new Version(1, 2);
//Retrieve Assembly Path
string pathForExe = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Program.exe");
//Delete taks if exists
taskService.RootFolder.DeleteTask("SomeTask", false);
// Create a new task definition and assign properties
TaskDefinition taskDefinition = taskService.NewTask();
//Security
taskDefinition.Principal.LogonType = TaskLogonType.InteractiveToken;
taskDefinition.Principal.UserId = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
//This property not supported in XP
if (IsNewer)
{
taskDefinition.Principal.RunLevel = TaskRunLevel.Highest;
}
taskDefinition.RegistrationInfo.Description = "Keeps your software up to date. If this task is disabled or stopped, your software will not be kept up to date, meaning security vulnerabilities that may arise cannot be fixed and features may not work. This task uninstalls itself when there is no software using it.";
// Add a trigger that, starting every month
// and Saturday and repeat every 10 minutes for the following 11 hours
MonthlyTrigger monthlyTrigger = new MonthlyTrigger();
monthlyTrigger.EndBoundary = DateTime.Now.AddYears(10);
taskDefinition.Triggers.Add(monthlyTrigger);
// Create an action that will launch Notepad whenever the trigger fires
taskDefinition.Actions.Add(new ExecAction(pathForExe));
// Register the task in the root folder
taskService.RootFolder.RegisterTaskDefinition("SomeTask", taskDefinition);
}
}
catch (Exception ex)
{
logger.Error(ex);
ServiceHelper.LogError(ex);
}
}
taskService.RootFolder.RegisterTaskDefinition("System Restore Checkpoint by System Restore Point Creator", newTask, TaskCreation.CreateOrUpdate, "NT AUTHORITY\System")
Dim ts As New TaskService()
' Create a new task definition and assign properties
Dim td As TaskDefinition = ts.NewTask()
td.RegistrationInfo.Description = "Does something"
td.Principal.RunLevel = TaskRunLevel.Highest
td.Principal.LogonType = TaskLogonType.InteractiveToken
' Create a trigger that will fire the task at 06:00 every day.
Dim dt As New DailyTrigger(1)
dt.StartBoundary = CDate(DateAdd(DateInterval.Day, 1, Now()).ToString("dd/MM/yyyy 06:00:00"))
td.Triggers.Add(dt)
'create a trigger that will fire the task every thursday at 04:00.
Dim wt As WeeklyTrigger = DirectCast(td.Triggers.Add(New WeeklyTrigger()), WeeklyTrigger)
wt.StartBoundary = CDate(DateAdd(DateInterval.Day, 1, Now()).ToString("dd/MM/yyyy 04:00:00"))
wt.DaysOfWeek = DaysOfTheWeek.Thursday
' Create an action that will launch Notepad whenever the trigger fires
td.Actions.Add(New ExecAction("notepad.exe", "c:\test.txt", Nothing))
' Register the task in the root folder
ts.RootFolder.RegisterTaskDefinition("TestTask", td)
Catch ex As Exception
MsgBox("error - " & ex.Message)
End Try
...and I'm trying to disable it using the following Try
Dim ts As New TaskService
Dim t As Task = ts.GetTask("TestTask")
t.Enabled = False
t.RegisterChanges()
Catch ex As Exception
MsgBox("error - " & ex.Message)
End Try
...but when I use the code to disable it, the code runs OK, but the task isn't disabled.Dim ts As New TaskService()
' Create a new task definition and assign properties
Dim td As TaskDefinition = ts.NewTask()
td.RegistrationInfo.Description = "Does something"
td.Principal.RunLevel = TaskRunLevel.Highest
td.Principal.LogonType = TaskLogonType.InteractiveToken
' Create a trigger that will fire the task at 06:00 every day.
td.Triggers.Add(New DailyTrigger(1) With {.StartBoundary = DateTime.Today.AddHours(6) })
' Create a trigger that will fire the task every Thursday at 04:00.
td.Triggers.Add(New WeeklyTrigger With {.StartBoundary = DateTime.Today.AddHours(4), .DaysOfWeek = DaysOfTheWeek.Thursday })
' Create an action that will launch Notepad whenever the trigger fires
td.Actions.Add("notepad.exe", "c:\test.txt")
' Register the task in the root folder
ts.RootFolder.RegisterTaskDefinition("TestTask", td)
'ask for the task name
Dim Taskname As String = InputBox("What's the task name?")
Dim td As TaskDefinition = Nothing
Try
'see if the username is set to something.
Dim ts As New TaskService
Dim thetask As Task = ts.GetTask(Taskname)
Dim Authorname As String = "NotGood"
Try
td = thetask.Definition
Authorname = td.RegistrationInfo.Author.ToString
Catch ex As Exception
Authorname = "NotGood"
End Try
'if we get a name, then this task was created by the 'wrong' user - try creating the task again to change the author.
If Authorname <> "NotGood" Then
td.Principal.RunLevel = TaskRunLevel.Highest
td.Principal.LogonType = TaskLogonType.InteractiveToken
td.RegistrationInfo.Author = "tevalis"
thetask.RegisterChanges()
ts.RootFolder.RegisterTaskDefinition(Taskname, td)
End If
Catch ex As Exception
MsgBox("Error - " & ex.Message)
End Try
This error is thrown by the base Microsoft library. I can try to help, but I may not be able to do anything. If you will send the code used to create the task definition, I may be able to spot the items that could be causing the problem.