VBScript to detect and close idle process using instr
-
Wednesday, February 27, 2013 6:10 PM
I found this script while searching through the forums and it does half of what I need, but not quite all... I would like to be able to enter a piece of the name of the process under sName and have it check if the process resembles any running and idle process and then kill it if it is. I'm thinking some form of instr and terminate() is the key. Then I can just change the sName at will to adapt it to any process I want killed. Hope this makes sense...
sName = "Excel"
iDelay = 10000
Set oWMIService = GetObject("winmgmts:\\.\root\CIMV2")
K = GetKernel
WScript.sleep(iDelay)
If GetKernel = K _
Then MsgBox sName & " appears to be idle" _
Else MsgBox sName & " is busy"Function GetKernel
Set cItems = oWMIService.ExecQuery("SELECT * FROM Win32_Process", , 48)
For Each oItem In cItems
if instr(1, oItem.Name, sName, 1) = 1 then _
GetKernel = oItem.KernelModeTime
Next
End Function
- Edited by Drivium Wednesday, February 27, 2013 6:21 PM
All Replies
-
Wednesday, February 27, 2013 7:25 PMYour question is a little unclear. Do you want to know how to use the InputBox method or the Instr function? If so then you will find the answers (plus some examples) in the help file script56.chm (downloadable from the Microsoft site).
-
Wednesday, February 27, 2013 7:50 PMNo, I don't need the input box. Instr SHOULD do it, just no luck in finding any form of these 3 elements together (instr, idle, terminate). I just want an easily modifiable script, where I just modify a key word in the script and it compares that keyword with the name of a running process, determines if it's idle, and kills it.
- Edited by Drivium Wednesday, February 27, 2013 7:52 PM
-
Wednesday, February 27, 2013 8:00 PMModerator
Wondering why you need to kill running processes?
Bill
-
Wednesday, February 27, 2013 8:10 PM
No, I don't need the input box. Instr SHOULD do it, just no luck in finding any form of these 3 elements together (instr, idle, terminate). I just want an easily modifiable script, where I just modify a key word in the script and it compares that keyword with the name of a running process, determines if it's idle, and kills it.
Here is the help file example for the instr function:
Dim SearchString, SearchChar, MyPos
SearchString ="XXpXXpXXPXXP" ' String to search in.
SearchChar = "P" ' Search for "P".
MyPos = Instr(4, SearchString, SearchChar, 1) ' A textual comparison starting at position 4. Returns 6.
MyPos = Instr(1, SearchString, SearchChar, 0) ' A binary comparison starting at position 1. Returns 9.
MyPos = Instr(SearchString, SearchChar) ' Comparison is binary by default (last argument is omitted). Returns 9.
MyPos = Instr(1, SearchString, "W") ' A binary comparison starting at position 1. Returns 0 ("W" is not found).If the example does not clarify the instr function then you need to specify more closely what you mean with "no luck". Posting what you tried would help.
- Proposed As Answer by OberwaldMicrosoft Community Contributor Thursday, February 28, 2013 5:37 PM
- Marked As Answer by IamMredMicrosoft Employee, Owner Tuesday, March 05, 2013 4:23 AM
-
Wednesday, February 27, 2013 10:11 PM
I will also say the the coe will not reliably determine an idle process. Many processes may be active and have no kernel time. Kernel tim is only used for ceratin high level actions. Basic Windows and other GUI porgrams and many services may appear to be idle at the kernel and be very active.
Idle time is not a useful tool. It is only useful when looking at the overall system. Idle time is the total time spent in the 'Idle" process which is a kernel service. It is not useful for guaging program usage.
Programs that need to do work need to be coded with a work timer that gtes reset periodically to detect when the process is free. Most programs do not do this.
Perhaps if you explain why you need to do this we can direct\ you to a good solution
.
¯\_(ツ)_/¯
-
Thursday, February 28, 2013 3:59 PMSure, we have a limited number of licenses that can be active simultaneously for a particular piece of software. Many times people will leave the app open, but aren't using it, preventing users who need it from accessing it. Plan to execute this AND the app from a batch file so it will be killed if not being used.
- Edited by Drivium Thursday, February 28, 2013 4:00 PM
-
Thursday, February 28, 2013 4:14 PMModerator
Usually it's not a good idea to kill processes, particularly for applications that open or write to files or databases.
It sounds more like a user training issue than a technological issue. Either that or purchase the needed number of licenses for the application so you don't have this problem in the first place.
Bill
- Proposed As Answer by Bill_StewartMicrosoft Community Contributor, Moderator Sunday, March 03, 2013 8:09 PM
- Marked As Answer by IamMredMicrosoft Employee, Owner Tuesday, March 05, 2013 4:23 AM
-
Thursday, February 28, 2013 5:21 PMOk, thanks for your time, everyone.

