Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Kill process problem

Status
Not open for further replies.

twooly

MIS
Feb 22, 2004
122
US
Trying to kill a process but won't let me kill it. I'm an Administrator on the box, so I am at loss. Using the code below I am getting "Error code 2" when it tries to kill it. Please note I am only trying to kill notepad.exe as a test.

Thanks

Code:
intPID = 3204   
strComputer = "server"
WScript.Echo "Process PID: " & intPID
set objWMIProcess = GetObject("winmgmts:\\" & strComputer & _
                    "\root\cimv2:Win32_Process.Handle='" & intPID & "'")
WScript.Echo "Process name: " & objWMIProcess.Name
intRC = objWMIProcess.Terminate() 
if intRC = 0 Then
   Wscript.Echo "Successfully killed process."
else
   Wscript.Echo "Could not kill process. Error code: " & intRC
end if
 
WScript.Echo "Process name: " & objWMIProcess.Name

gives you something you expect?

ms's code

Set colProcessList = objWMIService.ExecQuery _
("SELECT * FROM Win32_Process WHERE Name = 'notepad.exe'")
For Each objProcess in colProcessList
objProcess.Terminate()
Next

Return code Description
0 Successful completion
2 Access denied
3 Insufficient privilege
8 Unknown failure
9 Path not found
21 Invalid parameter
 
Yep it gives me exactly the values I was expected. This is weird. Using the code below this is the output I get. Yet it doesn't kill the process.

Code:
Process PID: 3204
Process name: notepad.exe
Successfully killed process.

Code:
intPID = 3204   
strComputer = "server"
WScript.Echo "Process PID: " & intPID

set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery _
    ("SELECT * FROM Win32_Process WHERE ProcessID =" & intPID)
    
For Each objProcess in colProcessList
WScript.Echo "Process name: " & objProcess.Name
    objProcess.Terminate()
Next

if intRC = 0 Then
   Wscript.Echo "Successfully killed process."
else
   Wscript.Echo "Could not kill process. Error code: " & intRC
end if
 
the pid is random? dont you want to use name notepad.exe? sorry i am assuming too much

sorry in your code intRC is not used to collect the return code from the terminate method...its was in your earlier posts
 
Opps forgot to set that back. Back to the Error code 2. I tried both the PID and Name both gave the Error Code 2.
 
Ok I figured out why but brings up another question with it. I had the process up and running under a different account than from the one I am kill from. So how can I kill the process even though its running as a different user? (I am an Administrator on the box)
 
hmm strange, never come across that personally but then i dont know if i have been in your exact situation,.#
have a look at the impersonate/security options for binding to WMI. but i think i am out of my depth here...sorry
 
>how can I kill the process even though its running as a different user? (I am an Administrator on the box)

>set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
[tt]set objWMIService = GetObject("winmgmts:[blue]{(debug)}[/blue]\\" & strComputer & "\root\cimv2")[/tt]
 
Same thing

Could not kill process. Error code: 2
 
Try elaborate a bit.
[tt]set objWMIService = GetObject("winmgmts:{impersonationLevel=Impersonate,(debug)}[blue]![/blue]\\" & strComputer & "\root\cimv2")[/tt]
 
Nope same exact thing. Its really weird. It works if the process is opened by the same user I am trying to kill it with but not if another user opened it.

Code:
strComputer = "server"

set objWMIService = GetObject("winmgmts:{impersonationLevel=Impersonate,(debug)}!\\" & strComputer & "\root\cimv2")
    
Set colProcessList = objWMIService.ExecQuery _
    ("SELECT * FROM Win32_Process WHERE Name ='notepad.exe'")
    
For Each objProcess in colProcessList
WScript.Echo "Process name: " & objProcess.Name
    intRC = objProcess.Terminate()
Next

if intRC = 0 Then
   Wscript.Echo "Successfully killed process."
else
   Wscript.Echo "Could not kill process. Error code: " & intRC
end if
 
Here is the Code that works very well for me.

Code:
' VB Script Document
option explicit
'#### Cleanup any left-over Excel processes ####'
Dim objProcess, colProcess, strComputer, objWMIService
Dim strProcessKill 
strComputer = "."
strProcessKill = "'excel.exe'"

Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _ 
& strComputer & "\root\cimv2") 

Set colProcess = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = " & strProcessKill )
For Each objProcess in colProcess
objProcess.Terminate()
Next 
WScript.Quit 
' End of WMI Example of a Kill Process

Also note that this script has a for/each loop and will kill all instances.

Hope this helps.


Thanks

John Fuhrman
Titan Global Services
 
Works for processes my id owns but not processes other users own.
 
Add your domain account (I suppose you log on the domain) to the server's local admin group.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top