viewpoint-particle

Author Topic: WMI - Terminating a process  (Read 622 times)

jtrask

  • Jr. Member
  • **
  • Posts: 68
WMI - Terminating a process
« 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? 

In either event, what do I do about it?

td

  • Tech Support
  • *****
  • Posts: 2141
    • WinBatch
Re: WMI - Terminating a process
« Reply #1 on: February 10, 2017, 02:33:22 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
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
 
"Success is a lousy teacher. It seduces smart people into thinking they can't lose."
  - Bill Gates


jtrask

  • Jr. Member
  • **
  • Posts: 68
Re: WMI - Terminating a process
« Reply #2 on: February 23, 2017, 07:19:03 am »
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.