Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use VBS to monitor CPU usage

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Editor to share with you how to use VBS to monitor the utilization of CPU, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's learn about it!

'--

On Error Resume Next

Dim iCpuUsePercentage' records CPU usage

Dim iSecond' records the duration for which the usage is equal to 100

Dim objFileStream'txt text read-write stream for logging

Dim objTextFileWriter'txt write object

Dim objShell'Shell' object, used to execute the specified program

Dim objFileInfo' file information object, used to get file size information

Whether dim bIsExecuteBat' has already run the bat file, only one batch will be executed before the next reset of 0

The initial value of iSecond=0' is 0

BIsExecuteBat=False

Set objFileStream = CreateObject ("Scripting.Filesystemobject") 'create a file system object

Set objTextFileWriter=objFileStream.opentextfile ("log.txt", 8 dint True) 'opens the log file by appending it in the same location in the script, or creates the log file if it does not exist

While True

Set objProc = GetObject ("winmgmts:\\.\ root\ cimv2:win32_processor='cpu0'") 'gets the first CPU object

ICpuUsePercentage=objProc.LoadPercentage' gets CPU utilization

ObjTextFileWriter.WriteLine ("[" & Now & "] CPU utilization:" & iCpuUsePercentage & "%, duration over 80%: & CStr (iSecond))

'count the number of times the cumulative CPU usage reaches more than 80. If the number is less than 80, the number is 0.

If iCpuUsePercentage > = 80 then

ISecond=iSecond+1

Else

ISecond=0

After bIsExecuteBat=False' resets 0, the batch can be executed again

End if

If iSecond > = 30 And bIsExecuteBat=False then

ObjTextFileWriter.WriteLine ("CPU usage reaches 80% for more than 30 seconds!")

'invoke the specified batch program

Set objShell=CreateObject ("WScript.shell")

ObjShell.run "c:\ email.bat"

The bIsExecuteBat=True' tag has already executed the bat file

End if

Set objFileInfo=objFileStream.getfile ("Log.txt")

'when the log file is greater than 2m, re-establish the log file

If (objFileInfo.size/1024/1024) > = 2 then

'close the file before you can operate

ObjTextFileWriter.close

ObjFileStream.MoveFile "Log.txt", Replace (Replace (Now, ":", "),"-","), ",") & "BackLog.txt"

'reopen the log file

Set objTextFileWriter=objFileStream.opentextfile ("log.txt", 8j True)

End if

Wend

'--

It is best to keep the log, which can help you find the problem. At present, it is 2m, and a new one will be created automatically after it is exceeded. Just clean it up regularly.

If you don't need a log, remove it.

ObjTextFileWriter.WriteLine ("[" & Now & "] CPU utilization:" & iCpuUsePercentage & "%, duration over 80%: & CStr (iSecond))

This one will do.

Communicate again if you have any questions, hehe!

-

The code has been modified. If it persists above 80%, the batch is executed only once, and once the count returns to zero, the batch can be executed again.

The above is all the contents of the article "how to use VBS to monitor CPU usage". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report