' Name: CPU_Use.vbs ' Purpose: Calculate CPU Usage ' Author: neilb ' Source: http://techrepublic.com.com/5208-6230-0.html?forumID=39&threadID=187114&messageID=1916629 ' Last Modified: 2007-09-22 ' Modified By: Jim Cameron ' Version: 1.1 ' Available from: ' http://support.moonpoint.com/downloads/computer_languages/VBScript/CPU_Use.vbs ' Changes: ' ' 1.1 Added variable specifying the time interval for checking the CPU ' utilization, which can now be specified as an argument, in seconds, ' to the script. ' Usage: ' ' "csript /nologo CPU_Use.vbs" or alternatively ' "cscript /nologo CPU_Use num" where "num" is the number of seconds to ' wait between CPU checks, e.g. "csript /nologo CPU_Use 300" to check every ' 5 minutes. If you don't specify a time interval, the default used will be ' 5 minutes. ' Notes: ' ' 1. This script requires Windows XP or later. It will not run on Windows 2000. ' 2. If the log file already exists, output will be appended to it. ' Output: ' ' The first two columns list the date and time the script was run while ' the third lists the CPU utilization at the time the script was executed. ' There is no value for CPU utilization for the first entry in the log. ' ' 9/22/2007 09:43 ' 9/22/2007 09:48 19 ' 9/22/2007 09:53 17 ' 9/22/2007 09:58 17 ' 9/22/2007 10:03 35 ' 9/22/2007 10:08 14 ' 9/22/2007 10:13 15 Const ForAppending = 8 'Contents not overwritten. ' Sleep interval in seconds (300 seconds = 5 min.). This is the default ' interval, if none is specified as an argument to the script. NumSecs = 300 strComputer = "." strLogFile = "c:\Processor.log" ' Log file where results are written strVersion = "1.1" ' Determine number of milliseconds to wait between CPU checks. ' The number of seconds to sleep between checks of the CPU utilization was ' specified as an argument to the script If Wscript.Arguments.Count = 1 Then NumSecs = WScript.Arguments(0) End If ' Sleep in milliseconds, which is what Wscript.Sleep actually uses NumMilSecs = NumSecs * 1000 Set objFSO=CreateObject("Scripting.FileSystemObject") Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") set objRefresher = CreateObject("WbemScripting.Swbemrefresher") Set objProcessor = objRefresher.AddEnum _ (objWMIService, "Win32_PerfFormattedData_PerfOS_Processor").objectSet objRefresher.Refresh Do For Each intProcessorUse in objProcessor If intProcessorUse.Name = "_Total" Then Set objLogfile = objFSO.OpenTextFile(strLogFile,ForAppending, true) objLogFile.WriteLine FormatDateTime(Now,vbShortDate) & vbTab & _ FormatDateTime(Now,vbShortTime) & vbTab & _ intProcessorUse.PercentProcessorTime objLogFile.Close Set objLogFile = Nothing End If Next Wscript.Sleep NumMilSecs ' Sleep in mSec. objRefresher.Refresh Loop