If LCase(Right(Wscript.FullName, 11)) = "wscript.exe" Then
strPath = Wscript.ScriptFullName
strCommand = "%comspec% /k cscript """ & strPath & """"
Set objShell = CreateObject("Wscript.Shell")
objShell.Run(strCommand), 1, True
Wscript.Quit
End If
intMaxAttempts = 50
Set objFSO = CreateObject("Scripting.FileSystemObject")
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set objPercent1 = CreateObject("Scripting.Dictionary")
Set objTime1 = CreateObject("Scripting.Dictionary")
Set objPercent2 = CreateObject("Scripting.Dictionary")
Set objTime2 = CreateObject("Scripting.Dictionary")
boolRequery = True
intAttempt = 0
On Error Resume Next
Err.Clear
While boolRequery = True And intAttempt <= intMaxAttempts
intAttempt = intAttempt + 1
Set processes = objWMIService.ExecQuery("select * from Win32_PerfRawData_PerfProc_Process")
For Each objProcess In processes
If objProcess.Name <> "Idle" And objProcess.Name <> "_Total" Then
If objPercent1.Exists(objProcess.Name) = True Then
objPercent1(objProcess.Name) = objPercent1(objProcess.Name) + objProcess.PercentProcessorTime
Else
objPercent1.Add objProcess.Name, objProcess.PercentProcessorTime
End If
If objTime1.Exists(objProcess.Name) = True Then
objTime1(objProcess.Name) = objTime1(objProcess.Name) + objProcess.Timestamp_Sys100NS
Else
objTime1.Add objProcess.Name, objProcess.Timestamp_Sys100NS
End If
End If
Next
WScript.Sleep 1000
Set processes = objWMIService.ExecQuery("select * from Win32_PerfRawData_PerfProc_Process")
For Each objProcess In processes
If objProcess.Name <> "Idle" And objProcess.Name <> "_Total" Then
If objPercent2.Exists(objProcess.Name) = True Then
objPercent2(objProcess.Name) = objPercent2(objProcess.Name) + objProcess.PercentProcessorTime
Else
objPercent2.Add objProcess.Name, objProcess.PercentProcessorTime
End If
If objTime2.Exists(objProcess.Name) = True Then
objTime2(objProcess.Name) = objTime2(objProcess.Name) + objProcess.Timestamp_Sys100NS
Else
objTime2.Add objProcess.Name, objProcess.Timestamp_Sys100NS
End If
End If
Next
' Go through each of the dictionary objects and add a zero entry for processes that may not have existed in each query
For Each strProcess In objPercent1
If objPercent2.Exists(strProcess) = False Then objPercent2.Add strProcess, 0
Next
For Each strProcess In objPercent2
If objPercent1.Exists(strProcess) = False Then objPercent1.Add strProcess, 0
Next
For Each strProcess In objTime1
If objTime2.Exists(strProcess) = False Then objTime2.Add strProcess, 0
Next
For Each strProcess In objTime2
If objTime1.Exists(strProcess) = False Then objTime1.Add strProcess, 0
Next
' Caclulate the usage
TotalCPUUsage = 0
HighestProcessName = ""
HighestProcessUsage = 0
For Each strProcess In objPercent1
'CPUPercent = ((objPercent2(strProcess) - objPercent1(strProcess)) / (objTime2(strProcess) - objTime1(strProcess))) * 100
CPUPercent = ((CDbl(objPercent2(strProcess)) - CDbl(objPercent1(strProcess))) / (CDbl(objTime2(strProcess)) - CDbl(objTime1(strProcess)))) * 100
WScript.Echo strProcess & ": " & CPUPercent
TotalCPUUsage = TotalCPUUsage + CPUPercent
If CPUPercent > HighestProcessUsage Then
HighestProcessUsage = CPUPercent
HighestProcessName = strProcess
End If
Next
TotalCPUUsage = Round(TotalCPUUsage, 3)
If Err.Number <> 0 Then
boolRequery = True
Else
boolRequery = False
End If
On Error GoTo 0
Wend
WScript.Echo VbCrLf & VbCrLf & "Highest Process: " & HighestProcessName & ": " & HighestProcessUsage
WScript.Echo VbCrLf & VbCrLf & "Total CPU Usage: " & TotalCPUUsage & VbCrLf & VbCrLf
strPath = Wscript.ScriptFullName
strCommand = "%comspec% /k cscript """ & strPath & """"
Set objShell = CreateObject("Wscript.Shell")
objShell.Run(strCommand), 1, True
Wscript.Quit
End If
intMaxAttempts = 50
Set objFSO = CreateObject("Scripting.FileSystemObject")
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set objPercent1 = CreateObject("Scripting.Dictionary")
Set objTime1 = CreateObject("Scripting.Dictionary")
Set objPercent2 = CreateObject("Scripting.Dictionary")
Set objTime2 = CreateObject("Scripting.Dictionary")
boolRequery = True
intAttempt = 0
On Error Resume Next
Err.Clear
While boolRequery = True And intAttempt <= intMaxAttempts
intAttempt = intAttempt + 1
Set processes = objWMIService.ExecQuery("select * from Win32_PerfRawData_PerfProc_Process")
For Each objProcess In processes
If objProcess.Name <> "Idle" And objProcess.Name <> "_Total" Then
If objPercent1.Exists(objProcess.Name) = True Then
objPercent1(objProcess.Name) = objPercent1(objProcess.Name) + objProcess.PercentProcessorTime
Else
objPercent1.Add objProcess.Name, objProcess.PercentProcessorTime
End If
If objTime1.Exists(objProcess.Name) = True Then
objTime1(objProcess.Name) = objTime1(objProcess.Name) + objProcess.Timestamp_Sys100NS
Else
objTime1.Add objProcess.Name, objProcess.Timestamp_Sys100NS
End If
End If
Next
WScript.Sleep 1000
Set processes = objWMIService.ExecQuery("select * from Win32_PerfRawData_PerfProc_Process")
For Each objProcess In processes
If objProcess.Name <> "Idle" And objProcess.Name <> "_Total" Then
If objPercent2.Exists(objProcess.Name) = True Then
objPercent2(objProcess.Name) = objPercent2(objProcess.Name) + objProcess.PercentProcessorTime
Else
objPercent2.Add objProcess.Name, objProcess.PercentProcessorTime
End If
If objTime2.Exists(objProcess.Name) = True Then
objTime2(objProcess.Name) = objTime2(objProcess.Name) + objProcess.Timestamp_Sys100NS
Else
objTime2.Add objProcess.Name, objProcess.Timestamp_Sys100NS
End If
End If
Next
' Go through each of the dictionary objects and add a zero entry for processes that may not have existed in each query
For Each strProcess In objPercent1
If objPercent2.Exists(strProcess) = False Then objPercent2.Add strProcess, 0
Next
For Each strProcess In objPercent2
If objPercent1.Exists(strProcess) = False Then objPercent1.Add strProcess, 0
Next
For Each strProcess In objTime1
If objTime2.Exists(strProcess) = False Then objTime2.Add strProcess, 0
Next
For Each strProcess In objTime2
If objTime1.Exists(strProcess) = False Then objTime1.Add strProcess, 0
Next
' Caclulate the usage
TotalCPUUsage = 0
HighestProcessName = ""
HighestProcessUsage = 0
For Each strProcess In objPercent1
'CPUPercent = ((objPercent2(strProcess) - objPercent1(strProcess)) / (objTime2(strProcess) - objTime1(strProcess))) * 100
CPUPercent = ((CDbl(objPercent2(strProcess)) - CDbl(objPercent1(strProcess))) / (CDbl(objTime2(strProcess)) - CDbl(objTime1(strProcess)))) * 100
WScript.Echo strProcess & ": " & CPUPercent
TotalCPUUsage = TotalCPUUsage + CPUPercent
If CPUPercent > HighestProcessUsage Then
HighestProcessUsage = CPUPercent
HighestProcessName = strProcess
End If
Next
TotalCPUUsage = Round(TotalCPUUsage, 3)
If Err.Number <> 0 Then
boolRequery = True
Else
boolRequery = False
End If
On Error GoTo 0
Wend
WScript.Echo VbCrLf & VbCrLf & "Highest Process: " & HighestProcessName & ": " & HighestProcessUsage
WScript.Echo VbCrLf & VbCrLf & "Total CPU Usage: " & TotalCPUUsage & VbCrLf & VbCrLf