- Oct 25, 2007
- 1,981
- 1
- 0
I've spent well over 12 hours searching and fighting with this travesty of a programming language to get this to work. Unfortunately, I'm stuck having to do this with just VBScript.
The major problems I'm running into are an inability to pull the proper CPU data, no array sort (will have to make one on my own apparently), no array slice (will have to make one on my own, fml).
I've been trying to get the data using WMI's Win32_PerfFormattedData_PerfProc_Process with the "Name" and "PercentProcessorTime" fields. Apparently you have to repoll the data twice just for it to give you valid information - If you just poll once it returns all zeros or NULL for PercentProcessorTime - which explains the retardation of this snippet:
If I go for less than 1 second sleep then I risk getting back all zero/null again.
Below is the code for my most recent attempt. The data at first seems fine until I try adding up the PercentProcessorTime and very quickly get over 100. More will be added to it once I can properly poll the data, like an array sort/slice.
Google has been almost no help whatsoever. I've cobbled the above together from about 15 different sources.
I could desperately use some help in getting this to work.
I've got the same exact script created in perl+linux that takes all of 2 lines of code (could be just 1 line through the miracle of shit-wizardry), but I can't for the life of me figure out how to do this on VBScript+Windows.
An additional and related question...
I will need a top 10 memory script as well. Once the basics are figured out with Proc this shouldn't be a problem, but will the same PerfFormatted data work just fine for this or will I need another WMI Object?
The major problems I'm running into are an inability to pull the proper CPU data, no array sort (will have to make one on my own apparently), no array slice (will have to make one on my own, fml).
I've been trying to get the data using WMI's Win32_PerfFormattedData_PerfProc_Process with the "Name" and "PercentProcessorTime" fields. Apparently you have to repoll the data twice just for it to give you valid information - If you just poll once it returns all zeros or NULL for PercentProcessorTime - which explains the retardation of this snippet:
Code:
objRefresher.Refresh
WScript.Sleep(1000)
objRefresher.Refresh
Below is the code for my most recent attempt. The data at first seems fine until I try adding up the PercentProcessorTime and very quickly get over 100. More will be added to it once I can properly poll the data, like an array sort/slice.
Code:
Option Explicit
Dim objWMIService, objRefresher
Dim strComputer,strList
Dim colProcesses,objProcess
strComputer = "."
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set objRefresher = CreateObject("WbemScripting.SWbemRefresher")
set colProcesses = objRefresher.AddEnum (objWMIService, "Win32_PerfFormattedData_PerfProc_Process").objectSet
objRefresher.Refresh
WScript.Sleep(1000)
objRefresher.Refresh
strList = "Name Proc"
for each objProcess in colProcesses
strList = strList & vbCr & objProcess.Name & " " & objProcess.PercentProcessorTime
next
WScript.Echo strList
I could desperately use some help in getting this to work.
I've got the same exact script created in perl+linux that takes all of 2 lines of code (could be just 1 line through the miracle of shit-wizardry), but I can't for the life of me figure out how to do this on VBScript+Windows.
An additional and related question...
I will need a top 10 memory script as well. Once the basics are figured out with Proc this shouldn't be a problem, but will the same PerfFormatted data work just fine for this or will I need another WMI Object?