Re: cpu usage?

C

Courage

Philip said:
<vbscript>

'Get % Processor Time for a process
'Connect to Local Machine
set wmi_service =
GetObject("winmgmts:{impersonationlevel=impersonate}!\root\cimv2")

sObjectPath = "Win32_PerfRawData_PerfProc_Process.Name=" & chr(34) &
"taskmgr" & chr(34)
wscript.echo sObjectPath

set perf_instance1 = wmi_service.get( sObjectPath )
N1 = perf_instance1.PercentProcessorTime
D1 = perf_instance1.TimeStamp_Sys100NS

while true

'Sleep for one second = 1000 ms
wscript.sleep(1000)

set perf_instance2 = wmi_service.get( sObjectPath )
N2 = perf_instance2.PercentProcessorTime
D2 = perf_instance2.TimeStamp_Sys100NS

' CounterType - PERF_100NSEC_TIMER
' Formula = ((N2 - N1) / (D2 - D1)) x 100

if ( 0 = (D2-D1) ) then
wscript.echo "divide by zero"
else
PercentProcessorTime = ((N2 - N1) / (D2 - D1)) * 100
wscript.echo "% Processor Time = " , PercentProcessorTime
end if

N1 = N2
D1 = D2
wend

set wmi_service = nothing

</vbscript>

This Code works great, just what I've been looking for.
The thing is, I'd like to do this using *"IDProcess"* and not by usin
*"Name"*.
Is this possible at all and if yes, how would I go about this


-
Courag
 
P

Philip Nunn [MSFT]

Since "Name" is the key property to identify instances of this class it is
required for GetObject. However, you can also query to obtain the desired
instance(s).

create a query like:
sQueryString = "select * from Win32_PerfRawData_PerfProc_Process where
IDProcess=''<your value>"
and replace the << set perf_instance = wmi_service.get(... >> code with

' sQueryString should be constructed to return only one instance, otherwise
arrays should be used for N1, N2
set objProcesses = wmi_service.ExecQuery( sQueryString )
for each objProc in objProcesses
N1 = objProc.PercentProcessorTime
N2 = ...
next
 
Joined
Sep 19, 2006
Messages
1
Reaction score
0
Hi guys the scripts works very good but I would like to know how that the formula works, besides I would like to know how the sleep works
'Sleep for one second = 1000 ms
>> wscript.sleep(1000)

because I´m looking that it takes about 40 seconds in execute the script I tried to change the time sleep, but the values the percentajes changes . Would exist any way way to reduce those times of execution?

Thanks

Other question is, If you know any script that runs so fast so good as the ps command. Im working with windows 2000 and in this enviroment the ps command doesn´t exit
and I would like to know any script that can give the list of the current process.

david_X

user_offline.gif
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top