Executing a remote process via WMI in Win32.

jose maria scasjos at mixmail.com
Thu Jul 10 14:41:28 CEST 2003

"Sean" <null-spam at tfh.ca> wrote in message news:<becdmd$hbo$1 at canopus.cc.umanitoba.ca>...
> I can connect to a machine remotely with no problems but I'm having trouble
> trying to create a process remotely.  Initially this was a perl and VB
> script which I'm converting to python.  Its entire purpose is to start a
> process remotely.  The problem initially was that the perl script could not
> 'see' any mapped drives as it would alway return 'access denied' so I'm
> trying to not only rewrite the script but fix this problem and my idea was
> to connect with the Administrator account.
> 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.
> ## being ##
> import win32com.client
> wmi = win32com.client.Dispatch('wbemscripting.swbemlocator')
> remote_machine =
> wmi.ConnectServer('<MACHINE>','root\\cimv2',Administrator','<PASSWORD>')
> process = remote_machine.InstancesOf("Win32_Process")
> for proc in process:
>     size = int(proc.WorkingSetSize)/1024
>     print proc.Caption, size,"kb"
> # 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:\\")
> ## END ##
> 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.
> Traceback (most recent call last):
>   File "C:\Documents and Settings\scody\test.py", line 14, in ?
>     process.Create("cmd.exe /K dir w:\\")
>   File "C:\Python23\lib\site-packages\win32com\client\dynamic.py", line 460,
> in
> __getattr__
>     raise AttributeError, "%s.%s" % (self._username_, attr)
> AttributeError: InstancesOf.Create
> The process method should only have the one parameter as defined at:
> http://userpages.umbc.edu/~kbradl1/wsz/ref/WMIref.html#process
> I just tried using the 'Terminate' process but that didn't work either....
> So I'm going to assume that the WMI objects are not instantiated as python
> classes (hence why the methods are not working).  Am I completely off base
> here (meaning it is a syntax problem)?
> Any suggestions or pointers to the right direction would be creately
> appriciated...

Hi Sean I´m now work in a module that create several kind of methods
to manage process using wmi take one example:

I´m use ActiveState Python 2.2.1 firts you have it´s that python know
of existencie of WMI for instance you must use COMMakepy utility now
Python *knows* about the 'WMI Scripting' typelibrary.

Note:this part of script its tested in windows 2000 and its functional
except in windows 2000 with SP2 that failed and I don´t know why

Example create process:
(After Use Makepy)
import win32com.client
wmi = win32com.client.Dispatch('wbemscripting.swbemlocator')
remote_machine =
strProcess=("cmd.exe /K & dir w:\\") #this is very important you must
put &
objprocess = remote_machine.Get('Win32_Process')
Method = objProcess.Methods_('Create')
InParameters = Method.InParameters.SpawnInstance_()
     OutParameters = objProcess.ExecMethod_ ('Create',InParameters)
     ReturnValue = OutParameters.Properties_.Item('ReturnValue').Value
     ProcessID = OutParameters.Properties_.Item('ProcessId').Value
except pythoncom.com_error, (hr,msg,exc,arg):

def ExceptionHandler (self,hr,msg,exc,arg):
            print 'COM call failed: %s' % msg
            if exc is not None:
                wcode, source, text, helpFile, helpID, scode = exc
                print 'Error source:',source
                print text
                print 'See also: %s (id=%d)' % (helpFile,helpID)

Example kill process:

strPid="'yourpid'" # In this form else not work
sql="SELECT * FROM  Win32_Process WHERE ProcessId=%s" %strPid 
            if objProc.Count==0:
                print "Pid strPid Not Found"
                for process in objProc:
                    strmeth = process.Methods_('Terminate')
Sorry Sean for my bad Enghlis I hope help you

More information about the Python-list mailing list