Search code examples
c#wmiwmi-query

Running WMI query on Win32_ScheduledJob returns no results


I'm trying to query a remote computer using WMI to get its scheduled tasks (two specifically) but the query returns nothing. I've tried running it against my local machine and it still returns no results. But if i query Win32_LogicalDisk it returns 3 results. To me that says WMI is working locally it just returns nothing for Scheduled jobs. I find that odd because when I run schtasks from a command prompt I get back about 25 tasks (maybe more) The following code is nothing fancy. I've commented out things I've tried, right now it is set to run against my machine locally.

public Win32_ScheduledJob QueryTask(string systemName, string p2)
{
    var job = new Win32_ScheduledJob();
    var connectionOptions = new ConnectionOptions()
    {
        Impersonation = ImpersonationLevel.Impersonate
    };
    var computer = string.Format(@"\\{0}\root\CIMV2", systemName);
    //var scope = new ManagementScope(computer);
    //scope.Connect();
    //var str = "SELECT * FROM Win32_LogicalDisk";
    var str = "SELECT * FROM Win32_ScheduledJob";
    var query = new ManagementObjectSearcher(str);
    var tasks = query.Get();
    //TODO search for Name==p2 and set its elements to job
    var count = tasks.Count;
    tasks.Dispose();
    return job;
}

any idea why I am getting no results? Oh i forgot to mention that I once used query.Get(???ManagementOO???) and subscribed to the 4 events and it calls completed with status of NoError but never calls Progress, ObjectReady, or ObjectPut.


Solution

  • The Win32_ScheduledJob class is internally using the AT protocol, which is bound to deprecation starting with Windows 8 and Windows Server 2012. As a first step the AT protocol is disabled by default. If the protocol is disabled, for example calling the Create method on a Win32_ScheduledJob object will fail with error 0x8. You can turn the AT protocol back on by adding the following registry entry:

    Key: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Schedule\Configuration 
    Name: EnableAt 
    Type: REG_DWORD
    Value: 1
    

    You may need to restart the machine to make the setting effective.

    Resource: Win32_ScheduledJob class

    Tried it with no success... Even after restart...

    FYI:

    ==>schtasks|find /C "TaskName"
    59