1

I'm writing a Python script that gets information on processes (PID, Command Line, etc.) running on a PC.

On my Windows 7 PC I can use 'wmic' and use the output from that:

output = subprocess.Popen(['wmic process get creationdate,commandline,processid'], stdout=subprocess.PIPE, shell=True).communicate()[0]

Example of the info I can get from this output:

PROCESS PID START TIME COMMAND: blah.exe 1234 Thu Jun 06 15:33:40 2013 C:\blah\blah.exe -a -b blah.txt

This gives me all the info I need... HOWEVER not all machines will let me use 'wmic' as I'm not an Administrator.

I can use 'tasklist', but it doesn't give me the 'commandline' info that wmic does.

I can't use 'psutils' (or the 'wmi' module), as it's not installed on any of the PCs I need it to work on, and I don't want to have to get admin install modules on all these PCs

Any ideas on how I can get the above process information using standard Python 2.6?

Thanks in advance, Nick

PYTHON 2.6 on Windows XP/7

=====================================================================

EDIT (12 June 2013):

I'm trying to do as suggested below, but having trouble... I've put the psutil directory (psutil-0.7.1) in the same directory as my script and added the following to my script:

path = '.\psutil-0.7.1'

sys.path.append(path)

import psutil

I now get the below error:

Traceback (most recent call last):

  File "C:\Users\nickw\Desktop\ProcessMonitor\ProcessMonitor_MODPS2.py", line 21, in <module>

    import psutil

  File ".\psutil-0.7.1\psutil\__init__.py", line 77, in <module>

    import psutil._psmswindows as _psplatform

  File ".\psutil-0.7.1\psutil\_psmswindows.py", line 15, in <module>

     import _psutil_mswindows

ImportError: No module named _psutil_mswindows

Any idea what I'm doing wrong?

  • psutils don't require admin rights - just place them anywhere, e.g. besides your program. – ivan_pozdeev Jun 06 '13 at 15:33
  • The same goes for `wmi` module (it requires `pywin32` though). – ivan_pozdeev Jun 06 '13 at 15:47
  • Thanks, though I'm having trouble doing that - see above... – Nick Fromage Jun 12 '13 at 15:26
  • Actually, I didn't know about a _python module_ called "psutil", I meant the sysinternals console utilities. The error says the `psutil` module requires another one, `_psutil_mswindows`. It's probably a DLL and the former module probably acts as a proxy for it. – ivan_pozdeev Jun 12 '13 at 20:59

1 Answers1

0

The technique to retrieve a process's command line is somewhat hacky but was included into WMI since XP. Basically, it's reading another process's memory. See e.g. Getting another process command line in Windows and links from there for details.

So, you either

  • re-implement it yourself by hand, with ctypes or whatever, like the link above says, or

  • somehow, utilize existing modules that will do that for you. There's zipimport to make this easier for multiple files and virtualenv to create whole environments anywhere on the filesystem, the latter likely being an overkill.

Community
  • 1
  • 1
ivan_pozdeev
  • 33,874
  • 19
  • 107
  • 152
  • Thanks very much for the help. Although I didn't use the solution you suggested, it made me realise that the 'very simple solution' that I hoped for wasn't there! So... I've managed to sort out using the WMI python module, which seems to work OK. Thanks again :-) – Nick Fromage Jun 13 '13 at 14:37
  • What I've actually done is installed the [WMI](https://pypi.python.org/pypi/WMI) python module on my PC and used it in my script. So that I don't have to install WMI on all the PCs I want to run it on, I've create a .exe from the .py script using [PyInstaller](http://www.pyinstaller.org/). Seems to work OK so far... – Nick Fromage Jun 13 '13 at 15:01