none
Win32_ScheduledJob is not returning any result in c# ? RRS feed

  • Question

  • hi all i am using below code to fetch the All Task Schedule in window 7
     
      ManagementScope scope = new ManagementScope("\\\\.\\ROOT\\cimv2");
    
                string query = "SELECT * FROM Win32_ScheduledJob";
    
                ManagementObjectSearcher searchCom;
                ManagementObjectCollection moCollection;
                WqlObjectQuery result = new WqlObjectQuery(query);
                searchCom = new ManagementObjectSearcher(scope,result);
                moCollection = searchCom.Get();
    

    but it is not returing any result.
    can anyone tell me what is wrong in above code?
     
    TIA



    sumitk

    Saturday, November 11, 2017 9:13 AM

Answers

  • The following code is ported from this MSDN documentation, an you need to add reference to "COM" -> "TaskScheduler 1.1 Type Library" (or something similar if you're on different version of Windows) in order to work.

                TaskScheduler.TaskScheduler service = new TaskScheduler.TaskScheduler();
                service.Connect();
    
                TaskScheduler.ITaskFolder rootFolder = service.GetFolder("\\");
                TaskScheduler.IRegisteredTaskCollection taskCollection = rootFolder.GetTasks(0);
    
                int numberOfTasks = taskCollection.Count;
    
                if (numberOfTasks == 0)
                {
                    Console.WriteLine("No tasks are registered.");
                }
                else
                {
                    Console.WriteLine("Number of tasks registered: " + numberOfTasks);
    
                    foreach (TaskScheduler.IRegisteredTask registeredTask in taskCollection)
                    {
                        string taskState;
                        Console.WriteLine("Task Name: " + registeredTask.Name);
                        switch (registeredTask.State)
                        {
                            case TaskScheduler._TASK_STATE.TASK_STATE_DISABLED:
                                {
                                    taskState = "Disabled";
                                }
                                break;
                            case TaskScheduler._TASK_STATE.TASK_STATE_QUEUED:
                                {
                                    taskState = "Queued";
                                }
                                break;
                            case TaskScheduler._TASK_STATE.TASK_STATE_READY:
                                {
                                    taskState = "Ready";
                                }
                                break;
                            case TaskScheduler._TASK_STATE.TASK_STATE_RUNNING:
                                {
                                    taskState = "Running";
                                }
                                break;
                            default:
                                {
                                    taskState = "Unknown";
                                }
                                break;
                        }
                        Console.WriteLine("Task State: " + taskState);
                    }
                }
    
                Console.ReadKey();


    • Edited by cheong00Editor Monday, November 13, 2017 6:22 AM
    • Marked as answer by Sumitk.cdac Tuesday, November 14, 2017 9:39 AM
    Monday, November 13, 2017 6:21 AM
    Answerer

All replies

  • Maybe you did not create such jobs using WMI. According to documentation for Win32_ScheduledJob, these jobs and the tasks that can be seen in Tasks Scheduler are managed separately.

    Sunday, November 12, 2017 5:14 PM
  • Hi,

    i am not creating any jobs using WMI. i just want to fetch the information.
    can you tell me the other way to fetch information of task schedule?


    sumitk

    Monday, November 13, 2017 3:53 AM
  • The following code is ported from this MSDN documentation, an you need to add reference to "COM" -> "TaskScheduler 1.1 Type Library" (or something similar if you're on different version of Windows) in order to work.

                TaskScheduler.TaskScheduler service = new TaskScheduler.TaskScheduler();
                service.Connect();
    
                TaskScheduler.ITaskFolder rootFolder = service.GetFolder("\\");
                TaskScheduler.IRegisteredTaskCollection taskCollection = rootFolder.GetTasks(0);
    
                int numberOfTasks = taskCollection.Count;
    
                if (numberOfTasks == 0)
                {
                    Console.WriteLine("No tasks are registered.");
                }
                else
                {
                    Console.WriteLine("Number of tasks registered: " + numberOfTasks);
    
                    foreach (TaskScheduler.IRegisteredTask registeredTask in taskCollection)
                    {
                        string taskState;
                        Console.WriteLine("Task Name: " + registeredTask.Name);
                        switch (registeredTask.State)
                        {
                            case TaskScheduler._TASK_STATE.TASK_STATE_DISABLED:
                                {
                                    taskState = "Disabled";
                                }
                                break;
                            case TaskScheduler._TASK_STATE.TASK_STATE_QUEUED:
                                {
                                    taskState = "Queued";
                                }
                                break;
                            case TaskScheduler._TASK_STATE.TASK_STATE_READY:
                                {
                                    taskState = "Ready";
                                }
                                break;
                            case TaskScheduler._TASK_STATE.TASK_STATE_RUNNING:
                                {
                                    taskState = "Running";
                                }
                                break;
                            default:
                                {
                                    taskState = "Unknown";
                                }
                                break;
                        }
                        Console.WriteLine("Task State: " + taskState);
                    }
                }
    
                Console.ReadKey();


    • Edited by cheong00Editor Monday, November 13, 2017 6:22 AM
    • Marked as answer by Sumitk.cdac Tuesday, November 14, 2017 9:39 AM
    Monday, November 13, 2017 6:21 AM
    Answerer
  • Thanks a lot :)

    it solved my problem

    can you please tell me how we can fetch values for triggers,Author and created properties which is shown in task schedule UI

    TIA


    sumitk


    • Edited by Sumitk.cdac Tuesday, November 14, 2017 11:41 AM
    Tuesday, November 14, 2017 9:39 AM