CPU Usage

Hints, tips and tricks for newbies

Moderators: Dorian (MJT support), JRL

Post Reply
Bazza1
Newbie
Posts: 1
Joined: Tue Apr 03, 2007 4:37 am

CPU Usage

Post by Bazza1 » Tue Apr 03, 2007 11:26 pm

I am trying to run a program during periods of low CPU Usage - Say when usage falls below 5% for 10sec.
Some days ago I found a script which I thought I could modify to do this, but have been unable to find it since!
Can anyone please help?
I am a newbie to scripting, so please keep the reply simple!!!

adroege
Automation Wizard
Posts: 438
Joined: Tue Dec 07, 2004 7:39 pm

VBScript to calculate CPU utilization

Post by adroege » Fri May 28, 2010 9:49 pm

If you have version 12 of Macro Scheduler, then you can implement the sleep(2000) inside of VBScript instead of having to use a Wait>2 which
will simplify the code somewhat.

This has only been tested on a one (1) processor machine.

But this example should give you an idea how to calculate the CPU usage of the computer.


[code]

VBStart

Function CPUUsage()
Set objWMIService = GetObject("Winmgmts:{impersonationlevel=impersonate}!\Root\Cimv2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_PerfRawData_PerfProc_Process where Name = 'Idle'")
For Each objItem In colItems
N1 = objItem.PercentProcessorTime
D1 = objItem.TimeStamp_Sys100NS
Next
CPUUsage = N1 & "," & D1
End Function
VBEnd

Let>comma=,

VBEval>CPUUsage(),N1D1
Separate>N1D1,comma,itemArray
Let>N1=%itemArray_1%
Let>D1=%itemArray_2%
Wait>2
VBEval>CPUUsage(),N2D2
Separate>N2D2,comma,itemArray
Let>N2=%itemArray_1%
Let>D2=%itemArray_2%

Let>Nd={%N2% - %N1%}
Let>Dd={%D2% - %D1%}
Let>PercentProcessorTime={( (%Nd% / %Dd%)) * 100}
VBEval>Round(%PercentProcessorTime%,0),PercentProcessorTime
Let>PercentProcessorTime=100-PercentProcessorTime
MessageModal>Processor Usage Percent = %PercentProcessorTime%


[/code]
Enjoy!

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