WMI remote call in python script to create process on remote windowscomputer

Discussion in 'Python' started by davidj411, Oct 5, 2009.

  1. davidj411

    davidj411 Guest

    import win32com.client
    computer = "server"
    strUser = "server\user_name"
    strPassword ="my_password"
    objSWbemLocator = win32com.client.Dispatch
    ("WbemScripting.SWbemLocator")
    objSWbemServices = objSWbemLocator.ConnectServer(computer, "root
    \cimv2",strUser,strPassword)
    objCreateProc = objSWbemServices.Get("Win32_Process")
    ProcessID = u"200"
    objCreateProc.Create(u"cabarc n c:\\temp\\test123.PY.cab c:\\temp\
    \dmj.new.log",u"c:\\temp",u' ',ProcessID )

    error returned:
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "C:\path\remote.process.py", line 20, in <module>
    objCreateProc.Create(u"cmd /c cabarc n c:\\temp\\test123.PY.cab c:\
    \temp\\new.log",u"c:\\temp",u'',ProcessID )

    what is causing this "int" error?

    i have tried this with and without Unicode.
    i'd like to do this without having a need to use Tim Golden's wmi
    module.
    davidj411, Oct 5, 2009
    #1
    1. Advertising

  2. davidj411

    Tim Golden Guest

    Re: WMI remote call in python script to create process on remotewindows computer

    davidj411 wrote:
    > import win32com.client
    > computer = "server"
    > strUser = "server\user_name"
    > strPassword ="my_password"
    > objSWbemLocator = win32com.client.Dispatch
    > ("WbemScripting.SWbemLocator")
    > objSWbemServices = objSWbemLocator.ConnectServer(computer, "root
    > \cimv2",strUser,strPassword)
    > objCreateProc = objSWbemServices.Get("Win32_Process")
    > ProcessID = u"200"
    > objCreateProc.Create(u"cabarc n c:\\temp\\test123.PY.cab c:\\temp\
    > \dmj.new.log",u"c:\\temp",u' ',ProcessID )
    >
    > error returned:
    > Traceback (most recent call last):
    > File "<stdin>", line 1, in <module>
    > File "C:\path\remote.process.py", line 20, in <module>
    > objCreateProc.Create(u"cmd /c cabarc n c:\\temp\\test123.PY.cab c:\
    > \temp\\new.log",u"c:\\temp",u'',ProcessID )
    >
    > what is causing this "int" error?


    Not clear which "int" error you mean, but trying to help anyway...

    You need to use raw strings (or use double-backslashes). That
    may or may not be the cause of your problems, but it certainly
    won't help. You may also run into theoretical restrictions of
    what remote WMI processes can achieve (no desktop/windowstation etc.).


    > i have tried this with and without Unicode.
    > i'd like to do this without having a need to use Tim Golden's wmi
    > module.


    To ask the obvious question: why? I'm not offended, just curious :)

    TJG
    Tim Golden, Oct 6, 2009
    #2
    1. Advertising

  3. davidj411

    Dave Angel Guest

    Re: WMI remote call in python script to create process on remotewindows computer

    davidj411 wrote:
    > import win32com.client
    > computer = "server"
    > strUser = "server\user_name"
    > strPassword ="my_password"
    > objSWbemLocator = win32com.client.Dispatch
    > ("WbemScripting.SWbemLocator")
    > objSWbemServices = objSWbemLocator.ConnectServer(computer, "root
    > \cimv2",strUser,strPassword)
    > objCreateProc = objSWbemServices.Get("Win32_Process")
    > ProcessID = u"200"
    > objCreateProc.Create(u"cabarc n c:\\temp\\test123.PY.cab c:\\temp\
    > \dmj.new.log",u"c:\\temp",u' ',ProcessID )
    >
    > error returned:
    > Traceback (most recent call last):
    > File "<stdin>", line 1, in <module>
    > File "C:\path\remote.process.py", line 20, in <module>
    > objCreateProc.Create(u"cmd /c cabarc n c:\\temp\\test123.PY.cab c:\
    > \temp\\new.log",u"c:\\temp",u'',ProcessID )
    >
    > what is causing this "int" error?
    >
    > i have tried this with and without Unicode.
    > i'd like to do this without having a need to use Tim Golden's wmi
    > module.
    >
    >

    As Tim says, you need to double your backslashes. You have several
    places where they are not properly escaped. Alternatively, at least in
    all these cases, you could use the raw strings.

    But the real problem with your message would seem to be that you retyped
    the material, with typos. And didn't include the entire error message.
    Copy/paste are your friend. And in case you don't know how to do that
    from a Windows console window, just ask. Or try Right-click on the
    title bar for some ideas.

    I don't have specific answers for you -- I don't currently do remote
    stuff this way. But you'll get better answers if you try the above.

    DaveA
    Dave Angel, Oct 6, 2009
    #3
  4. Re: WMI remote call in python script to create process on remotewindows computer

    ok, cut and pasted, but changed the username/password to protect the innocent.
    this is from interactive prompt.
    let me know if i am still not doing the slashes correctly please.
    i doubt authentication is the issue.; i can get pid information using
    WQL queries.
    objCreateProc.Create expects 4 strings (not objects?), right?

    version info:
    >>> sys.version

    '2.6 (r26:66721, Oct 2 2008, 11:35:03) [MSC v.1500 32 bit (Intel)]'

    >>> import win32com.client
    >>> computer = "servername"
    >>> strUser = "servername\\my_account"
    >>> strPassword ="shh_secret"
    >>> objSWbemLocator = win32com.client.Dispatch("WbemScripting.SWbemLocator")
    >>> objSWbemServices = objSWbemLocator.ConnectServer(computer, r"root\cimv2",strUser,strPassword)
    >>> objCreateProc = objSWbemServices.Get("Win32_Process")
    >>> ProcessID = u"200"
    >>> objCreateProc.Create(u"cmd /c ping 127.0.0.1 >>c:\\temp\\finall.log",u"c:\\temp",u' ',ProcessID )

    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    TypeError: 'int' object is not callable


    how can i see the method available?
    >>> help(objCreateProc) just gives me "Help on instance of CDispatch in module win32com.client:"


    Thanks
    David
    David Jackson, Oct 7, 2009
    #4
  5. davidj411

    Dave Angel Guest

    Re: WMI remote call in python script to create process on remotewindows computer

    David Jackson wrote:
    > ok, cut and pasted, but changed the username/password to protect the innocent.
    > this is from interactive prompt.
    > let me know if i am still not doing the slashes correctly please.
    > i doubt authentication is the issue.; i can get pid information using
    > WQL queries.
    > objCreateProc.Create expects 4 strings (not objects?), right?
    >
    > version info:
    >
    >>>> sys.version
    >>>>

    > '2.6 (r26:66721, Oct 2 2008, 11:35:03) [MSC v.1500 32 bit (Intel)]'
    >
    >
    >>>> import win32com.client
    >>>> computer = "servername"
    >>>> strUser = "servername\\my_account"
    >>>> strPassword ="shh_secret"
    >>>> objSWbemLocator = win32com.client.Dispatch("WbemScripting.SWbemLocator")
    >>>> objSWbemServices = objSWbemLocator.ConnectServer(computer, r"root\cimv2",strUser,strPassword)
    >>>> objCreateProc = objSWbemServices.Get("Win32_Process")
    >>>> ProcessID = u"200"
    >>>> objCreateProc.Create(u"cmd /c ping 127.0.0.1 >>c:\\temp\\finall.log",u"c:\\temp",u' ',ProcessID )
    >>>>

    > Traceback (most recent call last):
    > File "<stdin>", line 1, in <module>
    > TypeError: 'int' object is not callable
    >
    >
    > how can i see the method available?
    >
    >>>> help(objCreateProc) just gives me "Help on instance of CDispatch in module win32com.client:"
    >>>>

    >
    > Thanks
    > David
    >
    >

    Looks to me like

    objCreateProc.Create has an integer value.

    You could add a print for that value before the call to objCreateProc.Create(). (Leave off the parentheses on the print statement)

    My guess is that you've got an address there, but I have no idea how to convert that to a valid Python function pointer. Is there reasonable docs for WMI somewhere?

    DaveA
    Dave Angel, Oct 8, 2009
    #5
  6. davidj411

    Tim Golden Guest

    Re: WMI remote call in python script to create process on remotewindows computer

    David Jackson wrote:
    > ok, cut and pasted, but changed the username/password to protect the innocent.
    > this is from interactive prompt.
    > let me know if i am still not doing the slashes correctly please.
    > i doubt authentication is the issue.; i can get pid information using
    > WQL queries.
    > objCreateProc.Create expects 4 strings (not objects?), right?


    Invoking a method isn't as easy as that, I'm afraid. At
    the risk of being boring, can I ask again why you aren't
    using the wmi module, which solves these problems for
    you?

    In short you want to do something like this:
    (simplified for testing purposes to use the local machine)

    <code>
    import win32com.client

    wmi = win32com.client.GetObject ("winmgmts:")
    win32_process = wmi.Get ("Win32_Process")
    in_parameters = win32_process.Methods_ ("Create").InParameters
    in_parameters.Properties_ ('CommandLine').Value = "notepad.exe"
    result = win32_process.ExecMethod_ ("Create", in_parameters)

    </code>



    TJG
    Tim Golden, Oct 8, 2009
    #6
  7. Re: WMI remote call in python script to create process on remotewindows computer

    On Oct 8, 10:22 am, Tim Golden <> wrote:
    > David Jackson wrote:
    > > ok, cut and pasted, but changed the username/password to protect the innocent.
    > > this is from interactive prompt.
    > > let me know if i am still not doing the slashes correctly please.
    > > i doubt authentication is the issue.; i can get pid information using
    > > WQL queries.
    > > objCreateProc.Create expects 4 strings (not objects?), right?

    >
    > Invoking a method isn't as easy as that, I'm afraid. At
    > the risk of being boring, can I ask again why you aren't
    > using the wmi module, which solves these problems for
    > you?
    >
    > In short you want to do something like this:
    > (simplified for testing purposes to use the local machine)
    >
    > <code>
    > import win32com.client
    >
    > wmi = win32com.client.GetObject ("winmgmts:")
    > win32_process = wmi.Get ("Win32_Process")
    > in_parameters = win32_process.Methods_ ("Create").InParameters
    > in_parameters.Properties_ ('CommandLine').Value = "notepad.exe"
    > result = win32_process.ExecMethod_ ("Create", in_parameters)
    >
    > </code>
    >
    > TJG


    Good point, I would like just to add this:
    wmi = win32com.client.GetObject ("winmgmts:\\\\username:password@host\
    \root\\cimv2")
    and it is perfect :)
    Processor-Dev1l, Oct 8, 2009
    #7
  8. davidj411

    Tim Golden Guest

    Re: WMI remote call in python script to create process on remotewindows computer

    Processor-Dev1l wrote:
    > On Oct 8, 10:22 am, Tim Golden <> wrote:
    >> David Jackson wrote:
    >>> ok, cut and pasted, but changed the username/password to protect the innocent.
    >>> this is from interactive prompt.
    >>> let me know if i am still not doing the slashes correctly please.
    >>> i doubt authentication is the issue.; i can get pid information using
    >>> WQL queries.
    >>> objCreateProc.Create expects 4 strings (not objects?), right?

    >> Invoking a method isn't as easy as that, I'm afraid. At
    >> the risk of being boring, can I ask again why you aren't
    >> using the wmi module, which solves these problems for
    >> you?
    >>
    >> In short you want to do something like this:
    >> (simplified for testing purposes to use the local machine)
    >>
    >> <code>
    >> import win32com.client
    >>
    >> wmi = win32com.client.GetObject ("winmgmts:")
    >> win32_process = wmi.Get ("Win32_Process")
    >> in_parameters = win32_process.Methods_ ("Create").InParameters
    >> in_parameters.Properties_ ('CommandLine').Value = "notepad.exe"
    >> result = win32_process.ExecMethod_ ("Create", in_parameters)
    >>
    >> </code>
    >>
    >> TJG

    >
    > Good point, I would like just to add this:
    > wmi = win32com.client.GetObject ("winmgmts:\\\\username:password@host\
    > \root\\cimv2")
    > and it is perfect :)


    I wasn't aware of that form of moniker construction
    (ie with the username / password embedded) and on
    an XP-to-2k3 link it doesn't seem to work. Is it a
    Vista / 2k8 extension? Obviously, above, I was relying
    on the fact that the default default namespace (as opposed
    to the DEFAULT namespace ;) ) is root\cimv2.

    TJG
    Tim Golden, Oct 8, 2009
    #8
    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. Sean
    Replies:
    1
    Views:
    1,225
    jose maria
    Jul 10, 2003
  2. Marc Wyburn

    python create WMI instances

    Marc Wyburn, Jun 10, 2005, in forum: Python
    Replies:
    0
    Views:
    294
    Marc Wyburn
    Jun 10, 2005
  3. Tim Golden

    RE: python create WMI instances

    Tim Golden, Jun 10, 2005, in forum: Python
    Replies:
    2
    Views:
    515
  4. Tim Golden

    RE: python create WMI instances

    Tim Golden, Jun 22, 2005, in forum: Python
    Replies:
    1
    Views:
    381
  5. Mark
    Replies:
    2
    Views:
    305
Loading...

Share This Page