Monitor A Process

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
fthomas
Pro Scripter
Posts: 91
Joined: Fri Oct 03, 2008 6:40 pm

Monitor A Process

Post by fthomas » Thu Sep 25, 2014 1:41 am

Is there a way to monitor a particular process by it's name and get the amount of cpu usage that is occurring in percentage?

Thanks

Frank

User avatar
JRL
Automation Wizard
Posts: 3501
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Re: Monitor A Process

Post by JRL » Thu Sep 25, 2014 8:26 pm

Found a VBScript HERE. Had to modify it a bit to work in Macro Scheduler then had to divide the result by 2 to get the same result I see in Task Manager. Can't vouch for its accuracy but on my old WinXP laptop it is giving precisely the same CPU percentage as is being displayed in Task Manager at the moment I run the script. As posted the script is looking at FireFox.

Note: Adding .exe to the process name causes the script to report "0" so leave off the .exe from the process name.

Code: Select all

VBSTART
Function CPUUSage(ProcName)
  On Error Resume Next
  Set objService = GetObject("Winmgmts:{impersonationlevel=impersonate}!\Root\Cimv2")
   For Each objInstance1 in objService.ExecQuery("Select * from Win32_PerfRawData_PerfProc_Process where Name = '" & ProcName & "'")
       N1 = objInstance1.PercentProcessorTime
       D1 = objInstance1.TimeStamp_Sys100NS
     Exit For
   Next
WScript.Sleep(1000)
   For Each perf_instance2 in objService.ExecQuery("Select * from Win32_PerfRawData_PerfProc_Process where Name = '" & ProcName & "'")
       N2 = perf_instance2.PercentProcessorTime
       D2 = perf_instance2.TimeStamp_Sys100NS
     Exit For
   Next
    Nd = (N2 - N1)
    Dd = (D2 - D1)
    PercentProcessorTime = ((Nd/Dd)/2) * 100
  CPUUSage = Round(PercentProcessorTime ,0)
End Function
VBEND

vbeval>CPUUSage("Firefox"),res

MDL>res

fthomas
Pro Scripter
Posts: 91
Joined: Fri Oct 03, 2008 6:40 pm

Re: Monitor A Process

Post by fthomas » Thu Sep 25, 2014 10:17 pm

JRL, you rock!

Thank you so much.

Frank

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts