WMI - Terminating a process

Started by jtrask, February 10, 2017, 12:09:34 PM

Previous topic - Next topic

jtrask

I want to use WMI to terminate a process on a remote computer.  There's an example in the tech support database (http://techsupt.winbatch.com/webcgi/webbatch.exe?techsupt/nftechsupt.web+WinBatch/WMI+Close~All~but~One~Instance.txt) that uses

objProcess.Terminate(0)

I've tried this, but I get 1258: COM: Unknown name.

Is this because Terminate is being interpreted as a Winbatch command and not a method of the Win32_Process class? 

In either event, what do I do about it?

td

Quote from: jtrask on February 10, 2017, 12:09:34 PM
I want to use WMI to terminate a process on a remote computer.  There's an example in the tech support database (http://techsupt.winbatch.com/webcgi/webbatch.exe?techsupt/nftechsupt.web+WinBatch/WMI+Close~All~but~One~Instance.txt) that uses

objProcess.Terminate(0)

I've tried this, but I get 1258: COM: Unknown name.

Is this because Terminate is being interpreted as a Winbatch command and not a method of the Win32_Process class? 

Nope.  When I run Deana's script on my workstation it performs exactly as expected.

Quote
In either event, what do I do about it?

The error may be because the collection is not giving you an object for some reason.  The way to test this is to use the ObjectTypeGet function to check for an object type of "DISPATCH".  If the return value from ObjectTypeGet is not that skip to the next object or jump out of the loop completely.  Maybe something like this:

Code (winbatch) Select
Run('notepad','')
Run('notepad','')
appname = "notepad.exe" ; root filename of the process

objWMIService = ObjectGet("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
strQuery = `SELECT * FROM Win32_Process WHERE SessionId=1 AND Name = "`:appname:`"`
colProcesses = objWMIService.ExecQuery( strQuery )
mycount = colProcesses.Count
ForEach objProcess In colProcesses
    If mycount == 1 Then Break
    if ObjectTypeGet(objProcess) != "DISPATCH" then continue
    ;Message("Process ":objProcess.name, "Process Id ":objProcess.ProcessId:" Session ID ":objProcess.SessionId)
    objProcess.Terminate(0)
    mycount = mycount-1
Next
colProcesses  = 0
objWMIService = 0
Exit
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

jtrask

You got me straightened out.  My issue was that I cut/pasted from another UDF that was querying WIN32_PerfFormattedData_PerfProc_Process and NOT Win32_Process. 

I'll add this to my long list of rookie mistakes I've made this month.