Forum Discussion

scsvel's avatar
scsvel
Frequent Contributor
13 years ago

VBScript - Get CPU Usage

Hi all,
I want to get CPU Usage from VBScript. I can't use TC API that's why I am getting like this. Here is my code.

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set AllItems = objWMIService.ExecQuery("SELECT * FROM Win32_PerfFormattedData_PerfOS_Processor",,48)
for each itm in AllItems
WScript.Echo itm.PercentProcessorTime & "%"
next

Here PercentProcessorTime gives CPU Time not CPU Usage. I want to get CPU Usage. Can anybody help me?
Refer the screenshot also. Also refer http://msdn.microsoft.com/en-us/library/aa394372(v=VS.85).aspx#methods

Thanks...

8 Replies

  • vex's avatar
    vex
    Contributor
    I use this in my scripts all the time.  This should work.



    Function sdGetCpu(proc)



      proc = aqString.Replace(proc, ".exe", "")

      proc = lcase(proc)

     

      Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")

      set objRefresher = CreateObject("WbemScripting.SWbemRefresher")

      Set colItems = objRefresher.AddEnum(objWMIService, "Win32_PerfFormattedData_PerfProc_Process").objectSet

      objRefresher.Refresh



      For Each objItem in colItems

        If lcase(objItem.Name) = proc Then

          objRefresher.Refresh

          sdGetCpu = objItem.PercentProcessorTime

        End If

      Next

     

    End Function
  • scsvel's avatar
    scsvel
    Frequent Contributor
    Thanks Vince.



    Is the PercentProcessorTime gives, CPU Usage?

    I think, its giving CPU Time. See the Task Manager screenshot attached in this thread.

    Correct me if I am wrong here.



    Thanks...
  • HKosova's avatar
    HKosova
    SmartBear Alumni (Retired)
    Hi Shanmugavel,



    Win32_PerfFormattedData_PerfOS_Processor.PercentProcessorTime in the script that you posted is the CPU usage -- per processors/cores indeed. I'm afraid, it's not clear from your post what issues you're having with this code.



    Could you please clarify which exactly CPU usage do you need? --

    - A list of CPU usage per each process (application) (marked 1 in the attached screenshot)

    - CPU usage for a specific process (application)

    - Total CPU usage (marked 2 on the attached screenshot)

    - Something else



    Also, why don't TestComplete's Sys.CPUUsage and Sys.Process(...).CPUUsage suit your needs? Is it because you are planning to run this code using wscript/cscript outside of TestComplete?
  • scsvel's avatar
    scsvel
    Frequent Contributor
    Yes, You are Correct Helen. I want to run the script outside the TC environment.



    Here I want to know the CPU Usage for a particular process like you listed as point No.2 in the list. i.e. Marked as 1 your screenshot but for a particular process.

    Here I am bit confusion like Is PercentProcessorTime is the CPU Usage for a particular process? If so, then I am done.



    Thanks...
  • scsvel's avatar
    scsvel
    Frequent Contributor
    Yes, Exactly I want Sys.Process("process").CPUUsage value using other scripts externally.
  • HKosova's avatar
    HKosova
    SmartBear Alumni (Retired)
    Hi Shanmugavel,



    Yes, Exactly I want Sys.Process("process").CPUUsage value using other scripts externally.
    That's exactly what Vince's example does.



    Here I am bit confusion like Is PercentProcessorTime is the CPU Usage for a particular process?
    Yes, it's CPU usage.





    By the way, as for general scripting and programming questions that are not directly related to TestComplete and automated testing, you will probably find better answers in specialized forums, such as Scripting Guys Forum or Stack Overflow.