Forum Discussion

scsvel's avatar
scsvel
Frequent Contributor
14 years ago
Solved

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:\\" & strCompute...
  • vex's avatar
    14 years ago
    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