Terminating a process

Anything VBScript-related, including Windows Script Host, WMI, ADSI, and more.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 15 years and 3 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
jimbo101974
Posts: 40
Last visit: Mon Jan 12, 2009 4:52 am

Terminating a process

Post by jimbo101974 »

What is wrong with my script? I am able to manually terminate the process but when I run this script the process will not terminate. It does find the process, tells me it is running and asks if I'd like to kill it. I click yes but nothing happens.Dim strComputer Dim objWMIService Dim colProcesses Dim objProcess Dim Response strComputer = "." Set objWMIService = GetObject("winmgmts:" &_"{impersonationLevel=impersonate}!"&strComputer&"rootcimv2")Set colProcesses = objWMIService.ExecQuery ("Select * from Win32_Process Where Name = 'AeXNSAgent.exe'")Do While (colProcesses.Count <> 0)For Each objProcess In colProcessesResponse = MsgBox ("The process " & objProcess.Name & " is currently running." & vbNewLine & vbNewLine & "Would you like to stop this process now?", vbInformation + vbYesNo, "Process Example")If Response = vbYes ThenobjProcess.TerminateEnd If WScript.Sleep 100 Next Set colProcesses = objWMIService.ExecQuery ("Select * from Win32_Process Where Name = 'AeXNSAgent.exe'") Loop
User avatar
jhicks
Posts: 1789
Last visit: Mon Oct 19, 2015 9:21 am

Terminating a process

Post by jhicks »

do you get any error messages?
User avatar
jhicks
Posts: 1789
Last visit: Mon Oct 19, 2015 9:21 am

Terminating a process

Post by jhicks »

I see that you are only sleeping for 100 MS before checking again. That may not be long enough. Try bumping it up to 1000
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Terminating a process

Post by jvierra »

YOu can't re-initialize a loop enumerator from inside teh loop.

Use a function to isolate so enumerator will be refreshed correctly.
User avatar
jhicks
Posts: 1789
Last visit: Mon Oct 19, 2015 9:21 am

Terminating a process

Post by jhicks »

if you intent is to terminate all running instances of the process you shouldn't need to refresh, code like this should work:strComputer = "." strprocess="notepad.exe"Set objWMIService = GetObject("winmgmts:" &_"{impersonationLevel=impersonate}!"&strComputer&"rootcimv2")Set colProcesses = objWMIService.ExecQuery ("Select * from Win32_Process Where Name = '" & strProcess &"'")If colProcesses.Count >0 Then Response = MsgBox ("The process " & strProcess & " is currently running." & vbNewLine & vbNewLine & "Would you like to stop this process now?",vbInformation + vbYesNo, "Process Example") If Response = vbYes Then For Each objProcess In colProcesses objProcess.Terminate Next End IfEnd IfUnless the process is something that keeps coming back from the dead.
User avatar
jimbo101974
Posts: 40
Last visit: Mon Jan 12, 2009 4:52 am

Terminating a process

Post by jimbo101974 »

The process does resurrect itself.Edit: I did try the script as is with my process name and again, It did find it, it just didn't terminate it.Also tried it with notepad and it would not terminate notepad either.jimbo1019742009-01-12 09:21:53
User avatar
jhicks
Posts: 1789
Last visit: Mon Oct 19, 2015 9:21 am

Terminating a process

Post by jhicks »

That's odd behavior if the code I gave you can't kill any and all running instances of notepad. In fact, something can't be right. Do you get any error messages or does nothing happen at all when you use my code with running instances of notepad?
User avatar
jimbo101974
Posts: 40
Last visit: Mon Jan 12, 2009 4:52 am

Terminating a process

Post by jimbo101974 »

Another update. It seems as though any process started by user name SYSTEM I cannot kill. As long as I use a process started by me it works ok. I am able to manually kill any process therefore I am quite puzzled.Thoughts?
User avatar
jhicks
Posts: 1789
Last visit: Mon Oct 19, 2015 9:21 am

Terminating a process

Post by jhicks »

I was starting to think this had to be a permissions problem. It might depend on the process. I used the SearchIndexer.exe process which was running under System with my code it killed the process.
User avatar
jhicks
Posts: 1789
Last visit: Mon Oct 19, 2015 9:21 am

Terminating a process

Post by jhicks »

You might want to take a look at a column and tool I did a while ago for managing processesredmondmag.com/columns/article.asp?EditorialsID=1860Download and open the HTA in script editor to see how I handled terminating processes with WMI
This topic is 15 years and 3 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked