0

I am using a simple vb script to get CPU0's load percentage, but the WMI enumeration is significantly slower on Windows Server 2016 then older versions and I need to optimize the speed.

Here's my short script:

Option Explicit
Dim objWMIService, processItems, objitem, loadpercentage
loadpercentage= 0

Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")

Set processItems = objWMIService.ExecQuery("SELECT * FROM Win32_Processor _
                   WHERE DeviceID='CPU0'", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)

For Each objItem In processItems 
    loadpercentage =  objItem.LoadPercentage
Next

There's a 10 second delay between setting processItems and entering the For Each loop. This same script on older versions of Windows Servers takes 2 seconds. Can anything be done to optimize the WMI enumeration? Thanks in advance.

  • 1
    Possible duplicate of [Why are WMI Queries so slow sometimes?](https://stackoverflow.com/questions/1423981/why-are-wmi-queries-so-slow-sometimes) – user692942 Mar 28 '19 at 08:50
  • If these [sugguestions](https://learn.microsoft.com/en-gb/windows/desktop/WmiSdk/improving-enumeration-performance) don't help and it's only on a specific machine I would look to that machines network stack being the issue rather than WMI. – user692942 Mar 28 '19 at 08:52

1 Answers1

0

Try to not use * in your WQL queries. No filtering by any property just slows WMI queries very much. Instead of that, specify exacly the properties you need and nothing else:

SELECT LoadPercentage FROM Win32_Processor WHERE DeviceID='CPU0'
NicoRiff
  • 4,803
  • 3
  • 25
  • 54