RE: python create WMI instances

Discussion in 'Python' started by Tim Golden, Jun 10, 2005.

  1. Tim Golden

    Tim Golden Guest

    [Marc Wyburn]
    |
    | Hi all, I am struggling with a vb - python code conversion. I'm using
    | WMI to create printers on remote machines using (in VB);
    |
    | set oPrinter = oService.Get("Win32_Printer").SpawnInstance_
    |
    | oPrinter.DriverName = strDriver
    | oPrinter.PortName = strPort
    | oPrinter.DeviceID = strPrinter
    | oPrinter.Put_(kFlagCreateOnly)
    |
    | In python I have logged onto the WMI service on the remote machine and
    | I can run things like c.new.AddPrinterConnection so I know that I am
    | connected and working OK. I don't get any errors when I create a new
    | object with SpawnInstance_ but when I try to set the value of
    | oPrinter.Drivername I get an error saying that the Drivername object
    | doesn't exist. Does anyone know how to set the values of the object
    | using either the method above or with the WMI module? I think the WMI
    | module only allows access to modify methods such ADDPrinterConnection
    | or Create (from Win32_Service).

    Not sure if I can help here or not, but just in case...

    As far as I understand you, the fact that you're creating
    on a remote machine is just an incidental, ie you'd have
    the same problem doing this locally.

    Part of the problem is that when VB does something
    like:

    oPrinter.DriverName = strDriver

    what's really happening behind the scenes is something
    like:

    oPrinter.Properties_ ("DriverName").Value = strDriver

    Now you can do that in Python. (In fact, that's what
    the wmi module does when it overrides __setattr__, followed
    by a Put_). So if you want to translate code fairly literally,
    then carry on as you were, but substitute the latter code for
    the former.

    Having said that, the wmi module *can* create new instances
    of classes. The problem is that I had/have little knowledge
    of how WMI works in this area, so what I've done may not
    be right. The method you're looking for is .new (aliased
    as .new_instance_of) and if you use help on that method,
    you'll get this:

    new(self, wmi_class) unbound wmi.WMI method
    Create a new <whatever>, typically something like
    Win32_Process, eg:

    c = wmi.WMI ("remote_machine")
    for p in c.Win32_Process (name="notepad.exe"): print p
    c.new ("Win32_Process").Create (CommandLine="notepad.exe")
    for p in c.Win32_Process (name="notepad.exe"): print p
    p.Terminate ()
    for p in c.Win32_Process (name="notepad.exe"): print p

    Now this example works, but I notice from the code
    that what I did to make it work was to remove the
    SpawnInstance_ which I had, and replace it by an
    instance retrieval. ie I just do a Get on the class.

    I'll try to find some examples around of what you're
    doing which, up till now, I've not really needed to
    do. Meanwhile, I hope the above info is of some use.

    Feel free to respond with questions or comments. This
    can only get clearer!

    TJG

    ________________________________________________________________________
    This e-mail has been scanned for all viruses by Star. 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
    ________________________________________________________________________
    Tim Golden, Jun 10, 2005
    #1
    1. Advertising

  2. Tim Golden

    Guest

    Create isn't a method of Win32_Printer so I couldn't get that to work.
    ..Create is a method of Win32_Process which (wait for it..) creates a
    new process. Unfortunatly there is no method to add a printer. the
    method .AddPrinterConnection will allow me to add a connection to an
    existing print share but I want to create a printer with the option of
    sharing it.

    I think I'll have to do this with spawninstance.





    Tim Golden wrote:
    > [Marc Wyburn]
    > |
    > | Hi all, I am struggling with a vb - python code conversion. I'm using
    > | WMI to create printers on remote machines using (in VB);
    > |
    > | set oPrinter = oService.Get("Win32_Printer").SpawnInstance_
    > |
    > | oPrinter.DriverName = strDriver
    > | oPrinter.PortName = strPort
    > | oPrinter.DeviceID = strPrinter
    > | oPrinter.Put_(kFlagCreateOnly)
    > |
    > | In python I have logged onto the WMI service on the remote machine and
    > | I can run things like c.new.AddPrinterConnection so I know that I am
    > | connected and working OK. I don't get any errors when I create a new
    > | object with SpawnInstance_ but when I try to set the value of
    > | oPrinter.Drivername I get an error saying that the Drivername object
    > | doesn't exist. Does anyone know how to set the values of the object
    > | using either the method above or with the WMI module? I think the WMI
    > | module only allows access to modify methods such ADDPrinterConnection
    > | or Create (from Win32_Service).
    >
    > Not sure if I can help here or not, but just in case...
    >
    > As far as I understand you, the fact that you're creating
    > on a remote machine is just an incidental, ie you'd have
    > the same problem doing this locally.
    >
    > Part of the problem is that when VB does something
    > like:
    >
    > oPrinter.DriverName = strDriver
    >
    > what's really happening behind the scenes is something
    > like:
    >
    > oPrinter.Properties_ ("DriverName").Value = strDriver
    >
    > Now you can do that in Python. (In fact, that's what
    > the wmi module does when it overrides __setattr__, followed
    > by a Put_). So if you want to translate code fairly literally,
    > then carry on as you were, but substitute the latter code for
    > the former.
    >
    > Having said that, the wmi module *can* create new instances
    > of classes. The problem is that I had/have little knowledge
    > of how WMI works in this area, so what I've done may not
    > be right. The method you're looking for is .new (aliased
    > as .new_instance_of) and if you use help on that method,
    > you'll get this:
    >
    > new(self, wmi_class) unbound wmi.WMI method
    > Create a new <whatever>, typically something like
    > Win32_Process, eg:
    >
    > c = wmi.WMI ("remote_machine")
    > for p in c.Win32_Process (name="notepad.exe"): print p
    > c.new ("Win32_Process").Create (CommandLine="notepad.exe")
    > for p in c.Win32_Process (name="notepad.exe"): print p
    > p.Terminate ()
    > for p in c.Win32_Process (name="notepad.exe"): print p
    >
    > Now this example works, but I notice from the code
    > that what I did to make it work was to remove the
    > SpawnInstance_ which I had, and replace it by an
    > instance retrieval. ie I just do a Get on the class.
    >
    > I'll try to find some examples around of what you're
    > doing which, up till now, I've not really needed to
    > do. Meanwhile, I hope the above info is of some use.
    >
    > Feel free to respond with questions or comments. This
    > can only get clearer!
    >
    > TJG
    >
    > ________________________________________________________________________
    > This e-mail has been scanned for all viruses by Star. 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
    > ________________________________________________________________________
    , Jun 22, 2005
    #2
    1. Advertising

  3. Tim Golden

    Guest

    Win32_Printer doesn't work with the Create method and
    AddPrinterConnection only lets me add a connection to a share. I'll
    try and work out how to set the printer object properties in the format
    suggested;

    oPrinter.Properties_ ("DriverName").Value = strDriver

    Cheers, MW.
    , Jun 22, 2005
    #3
    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. John Wohlbier
    Replies:
    2
    Views:
    366
    Josiah Carlson
    Feb 22, 2004
  2. Marc Wyburn

    python create WMI instances

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

    RE: python create WMI instances

    Tim Golden, Jun 22, 2005, in forum: Python
    Replies:
    1
    Views:
    389
  4. Tim Golden

    RE: python create WMI instances

    Tim Golden, Jun 23, 2005, in forum: Python
    Replies:
    2
    Views:
    1,551
  5. Replies:
    8
    Views:
    462
    James Stroud
    Jan 29, 2009
Loading...

Share This Page