[python-win32] Executing a remote process via WMI in Win32.

Moore, Paul Paul.Moore at atosorigin.com
Tue Jul 8 11:20:35 EDT 2003


From: Sean [mailto:null-python-win32 at tinfoilhat.ca]
> Now the following script sort of works.  I can get the set of running
> processes but it dies out when I try to create one.

First point - Create() is a method on Win32_Process, but "process" in
your code is a WMI emumerator (SWbemObjectSet), not an object.

> process = remote_machine.InstancesOf("Win32_Process")

> # test to see if we can 'see' what's on a mapped drive.
> # doesn't seem to go.
> process.Create("cmd.exe /K dir w:\\")

> The script will print out all the processes fine but once it get's to
> 'Create' it dies out with and error message I don't understand.

> AttributeError: InstancesOf.Create

Means that (approximately - there are a lot of levels of complexity here)
a SWbemObjectSet object doesn't have a Create method. It's the SWbemObject
object that (sort of) has a Create method.

> Any suggestions or pointers to the right direction would be creately
> appriciated...

If you look in MSDN, you'll find more incomprehensible gibberish than you
have any interest in knowing. But if you work at it, and ignore the fact
that there always seem to be 10 ways of doing anything, and only the most
complicated one seems to actually *work*, you can get there :-)

Here's a bit of code which spawns an instance of Notepad on the local
machine. Once you've followed that, you should be well away. And with luck,
you may be able to tweak it to work for you without needing to follow all
the details (I'm not being obtuse here - I just don't have an easy way of
testing exactly what you're doing, and in my experience, using this stuff
without understanding it is fragile...)

----------- Cut Here -----------------
from win32com.client import GetObject

# Get the "Win32_Process" object
process = GetObject("winmgmts:Win32_Process")

# Get an object which represents the input parameters of the "Create"
# method of the Win32_Process object
params = process.Methods_("Create").InParameters.SpawnInstance_()

# The "CommandLine" parameter wants to be "notepad.exe"
params.Properties_.Item("CommandLine").Value = "notepad.exe"

# Execute the Create method on the Win32Process object
process.ExecMethod_("Create", params)
----------- Cut Here -----------------

One particularly nasty issue is that method and attribute names are often
case sensitive, and the VB examples in MSDN don't always use the correct
case (as VB is *not* case sensitive).

Hope this helps,
Paul.

PS I tried starting a remote process on another computer with this,
   but I hit errors ("The RPC server is unavailable.") This may be
   because of the security settings on my server - I don't know,
   sorry. You'll have to experiment to get that bit working...



More information about the Python-win32 mailing list