Search code examples
vbscriptpid

How to output process id in vbs / why is this not working


I am an absolute noob in vbs but so far, i have modified a script, which scans all the running processes and if they fall under the 'application' then it's name is outputted to a csv file.

Set Word = CreateObject("Word.Application")
Dim fso
Set fso = WScript.CreateObject("Scripting.Filesystemobject")
Set f = fso.CreateTextFile("output.csv", 2)
Set Tasks = Word.Tasks
For Each obj in Tasks
   PID = obj.ProcessID
   If obj.Visible Then f.Writeline obj.Name & "," & PID
Next
Word.Quit

My aim is to, get a list of all application, and after reading the name decide which to terminate and kill it using it's process id (which you get from the csv file).

It is able to correctly output the name however if i try to output processID i keep getting this:

Object doesn't support this property or method:
'obj.ProcessID'
Code : 800A02B6

There is not much help online, most examples i see use x.processID to find processID.

Can someone tell me why it does not work in the above code.

Thanks.


Solution

  • When in doubt, read the documentation. Task objects don't have a property ProcessID. Better use WMI for enumerating processes:

    Set wmi = GetObject("winmgmts://./root/cimv2")
    For Each p In wmi.ExecQuery("SELECT * FROM Win32_Process")
      WScript.Echo p.ProcessId
    Next