Reputation: 1
I tried to get the Command Line of a running Process with Python. I managed to do it with Powershell but I need to get it with Python which is my Problem.
Working Powershell Code:
Get-CimInstance Win32_Process -Filter "name = 'process.exe'" | select CommandLine
I tried everything I found but still can't do it... It would be very nice if someone could help me.
Upvotes: 0
Views: 770
Reputation: 118
This is answered here: https://stackoverflow.com/a/20721781/18505766
import psutil
for process in psutil.process_iter():
cmdline = process.cmdline
if "main.py" in cmdline and "testarg" in cmdline:
# do something
EDIT: Sorry, actually this is a bit different.
But if you're on Windows, you can try the following:
import wmi
w=wmi.WMI()
name = "cmd.exe"
for process in w.Win32_Process():
if process.Name == name:
tmp1 = process.Commandline
tmp2 = tmp1.split(' ', 1)
args = tmp2[1]
break
print(args)
EDIT2: improved code. Important: only the first process' arguments found are stored in var 'args'!
Upvotes: 1