[python-win32] Executing a remote process via WMI in Win32.
Tim Golden
tim.golden at viacom-outdoor.co.uk
Tue Jul 8 10:55:15 EDT 2003
> Sean
[... needs to control processes via WMI ...]
Initial brief answer is: get hold of this module:
http://tgolden.sc.sabren.com/python/wmi.html
which eases the (not inconsiderable) pain of using WMI
within Python. I haven't played around with the kind
of authentication you need (ie Admin on remote machine)
since I have Domain Admin rights on all the machines
I'm interested in. It should work, however; I just haven't
tried it.
I've certainly terminated processes successfully. I tried
doing a quick CreateProcess and it didn't like it too much,
but I don't have lots of time to look into why at the moment.
Promise I'll try to later.
Here the first bit of your code more-or-less translated:
<code>
import wmi
computer = wmi.WMI ()
# computer = wmi.WMI ("<MACHINE NAME>")
for process in computer.Win32_Process ():
size = int (process.WorkingSetSize) / 1024
print process.Caption, size, "kb"
</code>
Just to show how you could terminate processes:
<code>
#
# Close down any notepad processes
# two seconds after they've been
# started.
# (Good party trick, eh?)
#
watcher = computer.watch_for (
notification_type="Creation",
wmi_class="Win32_Process",
delay_secs=2,
Name="notepad.exe"
)
while 1:
notepad_process = watcher ()
print "Found notepad"
notepad_process.Terminate ()
</code>
Note that the WMI module doesn't do much; it's
forte is in keeping the kind of plumbing code
hidden that you had to expose, and then doing
a bit of __getattr__ magic so that you can call
class instances directly, and do things like:
c = wmi.WMI ()
process = c.Win32_Process ("notepad.exe")[0]
print process.Terminate
to get a list of the parameters to the process
method, which must be passed by keyword. Play
around with the module it and look at the code;
you'll see what it can and can't do quite easily.
Apart from simply Googling on "create process WMI"
or some similar search term, I've found this site
to be a good starting point:
http://www.activxperts.com/activmonitor/windowsmanagement/wmi/samples/
Good luck, and I'll try to get hold of process creation when I can.
TJG
________________________________________________________________________
This e-mail has been scanned for all viruses by Star Internet. The
service is powered by MessageLabs. For more information on a proactive
anti-virus service working around the clock, around the globe, visit:
http://www.star.net.uk
________________________________________________________________________
More information about the Python-win32
mailing list