Terminating a process
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.
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.
- jimbo101974
- Posts: 40
- Last visit: Mon Jan 12, 2009 4:52 am
Terminating a process
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
Terminating a process
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
Terminating a process
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.
- jimbo101974
- Posts: 40
- Last visit: Mon Jan 12, 2009 4:52 am
Terminating a process
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
Terminating a process
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?
- jimbo101974
- Posts: 40
- Last visit: Mon Jan 12, 2009 4:52 am
Terminating a process
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?
Terminating a process
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.
Terminating a process
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