How to get history of scheduled task in c#?

view task scheduler logs windows 10
how do i view windows scheduled task logs
view task scheduler logs windows server 2012
windows 10 scheduled task log
task scheduler logs windows server 2016
task scheduler logs path
windows 10 task scheduler log file location
windows task scheduler log output

I want to get history of specific task from task scheduler using c#. Could any one help me.

Thanks in advance.

I think the reason Task Scheduler API doesn't allow access to task history is that it's kept in Event Log. When you check the task history in Task Scheduler you can see it's log name is Microsoft-Windows-TaskScheduler/Operational. Same logs can be accessed in Event Viewer (Applications and Service Logs -> Microsoft -> TaskScheduler -> Operational)

I tried to get those entries using EventLog class but apparently we can't. I found this post suggesting a different approach: https://social.msdn.microsoft.com/Forums/en-US/882df3d5-9a9b-437b-a6ab-e09873ce6ece/cant-access-event-log-for-tasks?forum=csharplanguage

I fiddled with the code just to see if it would work in this case and looks like it does:

EventLogReader log2 = new EventLogReader("Microsoft-Windows-TaskScheduler/Operational");

for (EventRecord eventInstance = log2.ReadEvent(); null != eventInstance; eventInstance = log2.ReadEvent())
{
    if (!eventInstance.Properties.Select(p => p.Value).Contains("\\{YOUR SCHEDULED TASK NAME}}"))
    {
        continue;
    }

    Console.WriteLine("-----------------------------------------------------");
    Console.WriteLine("Event ID: {0}", eventInstance.Id);
    Console.WriteLine("Publisher: {0}", eventInstance.ProviderName);

    try
    {
        Console.WriteLine("Description: {0}", eventInstance.FormatDescription());
    }
    catch (EventLogException)
    {
    }

    EventLogRecord logRecord = (EventLogRecord)eventInstance;
    Console.WriteLine("Description: {0}", logRecord.FormatDescription());
}

\{YOUR SCHEDULED TASK NAME}} can be found in details section of logs under EventData. This way I could out history for a specific task.

I don't think you can run LINQ queries and get entries between dates or only get the failed ones etc but it might be a starting point.

I hope this helps.

How to get history of scheduled task from scheduler task , I am working to create schedule task dynamically using asp.net c#. I have created task successfully but now I want to get history of specific  Powershell script to show scheduled tasks history details and its properties ? Using Forums > Scheduled Task name and the Service Account Name.

Download Microsoft.Win32.TaskScheduler.dll from following link;

http://taskscheduler.codeplex.com/releases/view/120747

And add it as a reference in your C# program. Below is code to get history for particular scheduled task;

    using (TaskService ts = new TaskService())
    {

        TaskEventLog log = new TaskEventLog("taskPath");
        List<ListViewItem> c = new List<ListViewItem>(100);

        foreach (TaskEvent item in log)
        {
            Console.WriteLine(item.Level);
            Console.WriteLine(item.TimeCreated);
            Console.WriteLine(item.EventId);
            Console.WriteLine(item.TaskCategory);
            Console.WriteLine(item.OpCode);
            Console.WriteLine(item.ActivityId);
        }
    }

How to see scheduled task logs?, The Scheduled Tasks itself provide detailed information on when a task will run and when it ran. The Event Logs might inform you with warnings and errors  To retrieve a list of active schedulers, use the internal method internal static TaskScheduler [] GetTaskSchedulersForDebugger (). This static method returns an array of all active TaskScheduler instances. You can then use GetScheduledTasksForDebugger on each scheduler instance to retrieve its list of scheduled tasks.

A slight variation to Volkan Paksoy's answer this will allow you to query remote computers on your network. I've also added usings for the disposable objects

void Main()
{
    using (var session = new EventLogSession("remote server name"))
    {
        GetCompletedScheduledTaskEventRecords(session, "Your scheduled tasks name")
            .OrderByDescending(x => x.TimeCreated)
            .Select(r => new { CompletedTime = r.TimeCreated, r.TaskDisplayName, Props = string.Join(" | ", r.Properties.Select(p => p.Value)) })
            .Dump("Said Tasks Completed"); //using linqpad's Dump method, this just outputs the results to the display
    }
}
//If you don't want completed tasks remove the second part in the where clause
private List<EventRecord> GetCompletedScheduledTaskEventRecords(EventLogSession session, string scheduledTask)
{
    const int TASK_COMPLETED_ID = 102;
    var logquery = new EventLogQuery("Microsoft-Windows-TaskScheduler/Operational", PathType.LogName, "*[System/Level=4]") { Session = session };
    return GetRecords(logquery, 
        x=> x.Properties.Select(p => p.Value).Contains($@"\{scheduledTask}") && x.Id == TASK_COMPLETED_ID).ToList();
}
private IEnumerable<EventRecord> GetRecords(EventLogQuery query, Func<EventRecord, bool> filter)
{
    using (var reader = new EventLogReader(query))
    {
        for (var record = reader.ReadEvent(); null != record; record = reader.ReadEvent())
        {
            if (!filter(record)) continue;

            yield return record;
        }
    }
}

scheduled task history missing, The history is stored in the following event-log: Microsoft-Windows-​TaskScheduler/Operational. You can see this log by opening the Windows  How to build your own task scheduler in C# Take advantage of a custom task scheduler to provide added functionalities over the default task scheduler and manage how tasks are scheduled in .Net .

Scheduled Task Status & Email Notification Based on , Hi , Currently i do have a requirement to develop a console based windows application which will check the status of all scheduled jobs  The Get-ScheduledTask cmdlet gets the task definition object of a scheduled task that is registered on a computer. Examples. This command gets the definition object of the SystemScan scheduled task in the root folder. This command gets an array of task definitions objects from the UpdateTasks folder.

TaskScheduler Class (System.Threading.Tasks), It creates a custom task scheduler that limits the number of threads used by the app. Count == 0) { --_delegatesQueuedOrRunning; break; } // Get the next item thread 3 'a' in task t1-4 on thread 3 'b' in task t1-4 on thread 3 // 'c' in task t1-4  The Get-ScheduledTaskInfo cmdlet gets the last run-time information for a scheduled task. You can use the TaskName parameter to specify a scheduled task, or you can use the InputObject parameter to specify the scheduled task. Examples. Example 1: Get run-time information by using a task name. PS C:\> Get-ScheduledTaskInfo -TaskName "\Sample\SchedTask01"

Task Scheduler - Cannot view task history - TechNet, Suddenly the Administrator account cannot view the task history of all It says: The user account does not have permission to view task history on permissions on C:\Windows\Tasks and C:\Windows\System32\Tasks folder. I am working to create schedule task dynamically using asp.net c#. I have created task successfully but now I want to get history of specific schedule task from task scheduler dynamically. Could any one help me. Thanks in advance.

Comments
  • I'm not sure you can. The API reference is at msdn.microsoft.com/en-us/library/windows/desktop/… but it doesn't mention history as far as I can see. Also the examples are all in scripting languages or C++, so I don't think you can access the API directly using C#.
  • Even Task Scheduler Managed Wrapper doesn't have any structure to read history part. I don't think there is a way to do it as well.
  • I have been programming C# for I don't know how many years and I have never thought of using a for loop like that for reading in data. That pattern is perfect for reading in any kind of "streamed" data where the last record has a special value. I might start using that for Stream.Read( loops.