Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

What I have tried:

I search on google but did not get any solution
Posted
Updated 2-Nov-18 12:54pm
Comments
Richard MacCutchan 11-Nov-17 6:22am    
Maybe there are no scheduled jobs.
sumitk.cadc 12-Nov-17 22:55pm    
i created job for fetching info

1 solution

The reason is that this call internally uses the old AT protocol (that is NT-style, like the old AT command), and not those created in say the Task Scheduler.

see e.g. [^]

But the obvious question on how we get to all the rest, on Win8/WinSrv2012 and beyond remotely I unfortunately does not have an answer for...

ADD:
Found a solution with this MicroSoft Nuget package:
using Microsoft.Win32.TaskScheduler;
using (TaskService ts = new TaskService($"\\\\{host}", username, domName, password)) {
    Task[] tasks = ts.FindAllTasks(new System.Text.RegularExpressions.Regex("."),true);
    foreach(Task task in tasks) {
        TaskPrincipal princ = task.Definition.Principal;
        Console.WriteLine($"{host} SchedJob:'{task.Name}' usr:'{princ.UserId}' Enabled:{task.Enabled}")
    }
}
 
Share this answer
 
v4
Comments
EskeRahn 5-Nov-18 5:49am    
Note that this (as the OP code) goes through RPC, which is not that often open on machines in general. i.e. NOT through the commonly open WinRM. See https://github.com/dahall/TaskScheduler/issues/785

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900