List running processes on 64-bit Windows

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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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:Windowsexplorer.exe
cmd.exe                    C:WindowsSysWOW64cmd.exe
conhost.exe                C:Windowssystem32conhost.exe
...

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

Method 2

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

Method 3

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()]

Method 4

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.

Method 5

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.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x