简体   繁体   English

在 Win32_ScheduledJob 上运行 WMI 查询不返回任何结果

[英]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.我正在尝试使用 WMI 查询远程计算机以获取其计划任务(特别是两个),但查询不返回任何内容。 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.但是如果我查询 Win32_LogicalDisk 它会返回 3 个结果。 To me that says WMI is working locally it just returns nothing for Scheduled jobs.对我来说,说 WMI 在本地工作,它只会为计划作业返回任何内容。 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.我觉得这很奇怪,因为当我从命令提示符运行schtasks时,我得到了大约 25 个任务(也许更多) 下面的代码没什么花哨的。 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.哦,我忘了提到我曾经使用 query.Get(???ManagementOO???) 并订阅了 4 个事件,它以 NoError 状态调用完成,但从不调用 Progress、ObjectReady 或 ObjectPut。

The Win32_ScheduledJob class is internally using the AT protocol, which is bound to deprecation starting with Windows 8 and Windows Server 2012 . Win32_ScheduledJob类在内部使用AT协议,从Windows 8Windows Server 2012开始必然会被弃用 As a first step the AT protocol is disabled by default.作为第一步,默认情况下禁用AT协议。 If the protocol is disabled, for example calling the Create method on a Win32_ScheduledJob object will fail with error 0x8 .如果协议被禁用,例如在Win32_ScheduledJob对象上调用Create方法将失败并出现错误0x8 You can turn the AT protocol back on by adding the following registry entry:您可以通过添加以下注册表项重新打开AT协议:

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 资源: Win32_ScheduledJob

Tried it with no success... Even after restart...试了没有成功...即使重新启动...

FYI:供参考:

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

Instead of /root/cimv2 change the namespace to /Root/Microsoft/Windows/TaskScheduler and instead of SELECT * FROM Win32_ScheduledJob change it to SELECT * FROM MSFT_ScheduledTask .而不是/root/cimv2将命名空间更改为/Root/Microsoft/Windows/TaskScheduler而不是SELECT * FROM Win32_ScheduledJob将其更改为SELECT * FROM MSFT_ScheduledTask Within the object/class MSFT_ScheduledTask pick your items like TaskName在对象/类MSFT_ScheduledTask选择您的项目,例如TaskName

One Example in vba/vbs: vba/vbs 中的一个示例:

Dim objItems As Object
Dim objItem As Object

Const wbemFlagReturnImmediately = &H10
Const wbemFlagForwardOnly = &H20

Set objItems = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\Root\Microsoft\Windows\TaskScheduler").ExecQuery("" & _
"SELECT * FROM MSFT_ScheduledTask", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)

For Each objItem In objItems
    Debug.Print objItem.TaskName
Next

Hint: If you execute Get-ScheduledTasks in Powershell you see the CimClass which I have used above.提示:如果您在 Powershell 中执行Get-ScheduledTasks ,您会看到我在上面使用的 CimClass。 You can transfer this knowledge to other cases where you know the CimClass and the object.您可以将此知识转移到您知道 CimClass 和对象的其他情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM