14

I amm writing a little python script that will grab information from VMs of Windows that I am running.

At the moment I can list the processes on a 32bit XP machine with the following method:

http://code.activestate.com/recipes/305279/

Is it possible to somehow detect the version of windows running and excute a different method for getting the processes on a 64bit machine, I am trying to get the processes from a 64Bit Vista and 64bit Windows 7.

Any ideas?

tshepang
  • 12,111
  • 21
  • 91
  • 136
RailsSon
  • 19,897
  • 31
  • 82
  • 105

5 Answers5

28

If you don't want to rely on any extra installed modules then you can parse the output of wmic, e.g.:

c:\> wmic process get description,executablepath    
...
explorer.exe               C:\Windows\explorer.exe
cmd.exe                    C:\Windows\SysWOW64\cmd.exe
conhost.exe                C:\Windows\system32\conhost.exe
...

Reference: http://geekpedia.wordpress.com/2008/08/18/use-command-line-to-track-windows-processes/

orip
  • 73,323
  • 21
  • 116
  • 148
27

There is another recipe on activestate that does a similar thing, but uses the Performance Data Helper library (PDH) instead.

I have tested this on my Windows 7 64bit machine and it works there - so presumably the same function will work on both 32bit and 64 bit windows.

You can find the recipe here: http://code.activestate.com/recipes/303339/

Another method is using WMI, there is an example here in Python using the wmi module:

http://timgolden.me.uk/python/wmi/cookbook.html

import wmi
c = wmi.WMI ()

for process in c.Win32_Process ():
  print process.ProcessId, process.Name
Andre Miller
  • 15,255
  • 6
  • 55
  • 53
  • Does WMI work on 64bit windows also? I can not see anything on there site that mentions 64bit, or do you use the same method names? – RailsSon Oct 27 '09 at 17:26
  • It should work the same way (including method) names for both environments. – Andre Miller Oct 27 '09 at 18:00
  • I have been utilizing this method in my application, but we are noticing significant CPU spikes when this lookup gets triggered. I originally thought it was the scheduler I am using, but I started commenting out code until I got down to this specific code. Commented out, no significant spikes. Any thoughts? – Sherd Feb 12 '19 at 21:39
  • I should add the approach Jonathan Rocher posted doesn't have this issue and has the benefit of being platform agnostic. – Sherd Feb 13 '19 at 16:15
  • `pip install wmi` seem to succeed and mirror the release on the linked website. – luckydonald Nov 23 '19 at 12:25
  • If you get an module not found error similar to "module win32something not found" or similar, install the right release of [pywin32](https://github.com/mhammond/pywin32/releases) from there. – luckydonald Nov 23 '19 at 12:38
11

The cleanest way I found to solve this was to use the psutil library as recommended by Robert Lujo:

psutil.process_iter()

Note that it returns a generator object, issuing a process object at a time. For example if you need the list of process names, you can do something like:

[p.name() for p in psutil.process_iter()]
Mathias711
  • 6,568
  • 4
  • 41
  • 58
jonathanrocher
  • 1,200
  • 7
  • 10
  • hi, that one's nice, just a question, as p is a process with pid and name, why do you get p.name() with brackets ? and why doesn't it work if I want to get the pids ? I'm trying to get the matching pid/name – A.Joly Sep 14 '17 at 09:00
9

For similar purposes I have used psutil library. Some hints:

  • list processes with psutil.pids() (reference)
  • inspect process information with process = psutil.Process(pid) (reference)
  • do process.kill or process.terminate()

Installation on windows - pip will do installation from the source (which means compiling), so you probably want to download binary installation from https://pypi.python.org/pypi/psutil/#downloads.

Robert Lujo
  • 15,383
  • 5
  • 56
  • 73
  • 5
    psutil limits its output of processes to the current user running the python script. Hence a non-admin user will not see privileged processes. – rosch Oct 07 '13 at 09:02
0

You should be able to do this by exposing Windows Management Instrumentation within each VM. This tool gives you access to a bunch of system data, including processes, see http://technet.microsoft.com/en-us/library/cc757287%28WS.10%29.aspx

You should be able to popen one of the commands in the preceding link to get the info you're looking for.

Dana the Sane
  • 14,762
  • 8
  • 58
  • 80
  • 1
    It is significantly better to use a tool like win32process or psutils than it is to pipe output from a command line command. Something that is intentionally integrated with python will always be easier and cleaner to use. – trevorKirkby Jan 07 '14 at 02:41