Executing a remote process via WMI in Win32.

Discussion in 'Python' started by Sean, Jul 7, 2003.

  1. Sean

    Sean Guest

    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.

    NOTEPAD.EXE 80 kb
    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...

    --
    Sean
    (ps. remove -spam to email me).
    Sean, Jul 7, 2003
    #1
    1. Advertising

  2. Sean

    jose maria Guest

    "Sean" <> wrote in message news:<becdmd$hbo$>...
    > 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.
    >
    > NOTEPAD.EXE 80 kb
    > 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 =
    wmi.ConnectServer('<MACHINE>','root\\cimv2',Administrator','<PASSWORD>')
    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_()
    InParameters.Properties_.Item('CommandLine').Value=strProcess
    try:
    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):
    ExceptionHandler(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
    objProc=objprocess.ExecQuery(sql)
    if objProc.Count==0:
    print "Pid strPid Not Found"
    else:
    for process in objProc:
    strmeth = process.Methods_('Terminate')
    strparms=strmeth.InParameters
    strparms.Properties_('Reason').Value=0
    process.ExecMethod_('Terminate',strparms)

    Sorry Sean for my bad Enghlis I hope help you
    jose maria, Jul 10, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    558
    Bobby Ryzhy
    Aug 5, 2004
  2. davidj411
    Replies:
    7
    Views:
    3,263
    Tim Golden
    Oct 8, 2009
  3. Richard Lamboj

    win32 - catch events(wmi?)

    Richard Lamboj, May 3, 2010, in forum: Python
    Replies:
    0
    Views:
    222
    Richard Lamboj
    May 3, 2010
  4. jpock76
    Replies:
    0
    Views:
    610
    jpock76
    Aug 23, 2010
  5. Daniel Berger

    Win32::OLE, WMI and executing methods

    Daniel Berger, Jun 3, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    483
    Daniel Berger
    Jun 4, 2004
Loading...

Share This Page