win32com (VBScript to Python) problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tim Howarth

    win32com (VBScript to Python) problem



    I'm (very non expert) trying to use a snippet of VBScript (to shut down
    Windows workstations) converted to Python but have hit a problem.


    The VBScript below work fine;


    pc="MyPC"

    Set oWMI=GetObject( "winmgmts:{impe rsonationLevel= impersonate,(Sh utdown)}!\\"_
    & pc & "\root\cimv 2")

    Set colOperatingSys tems = oWMI.ExecQuery( "Select * from Win32_Operating System")

    For Each obj in colOperatingSys tems
    obj.Win32shutdo wn 12
    Next



    but my translation results in "'int' object is not callable" with the
    attempted call to Win32Shutdown.

    This is with Python 2.3.2 win32all 1.57 or Active Python 2.3.2

    print type(obj.Win32S hutDown)
    gives int

    print obj.Win32ShutDo wn
    gives 87 - the ASCII code of "W" - coincidentally ?




    import win32com.client , sys


    pc='MyPC'

    oWMI =
    win32com.client .GetObject(r"wi nmgmts:{imperso nationLevel=imp ersonate,\
    (Shutdown)}!\\" + pc + r"\root\cimv 2")

    colOperatingSys tems = oWMI.ExecQuery( r"Select * from Win32_Operating System")

    for obj in colOperatingSys tems:
    obj.Win32ShutDo wn(12)




    --
    ___
    |im ---- ARM Powered ----
  • Rony Steelandt

    #2
    Re: win32com (VBScript to Python) problem

    Not knowing exactly what you want to do, just a question.

    Why use COM, shutdown is an exe so why not just execute it?

    Rony


    "Tim Howarth" <tim@worthy.dem on.co.uk> a écrit dans le message de news:
    d715dc564c.tim@ worthy.demon.co .uk...[color=blue]
    >
    >
    > I'm (very non expert) trying to use a snippet of VBScript (to shut down
    > Windows workstations) converted to Python but have hit a problem.
    >
    >
    > The VBScript below work fine;
    >
    >
    > pc="MyPC"
    >
    > Set[/color]
    oWMI=GetObject( "winmgmts:{impe rsonationLevel= impersonate,(Sh utdown)}!\\"_[color=blue]
    > & pc & "\root\cimv 2")
    >
    > Set colOperatingSys tems = oWMI.ExecQuery( "Select * from[/color]
    Win32_Operating System")[color=blue]
    >
    > For Each obj in colOperatingSys tems
    > obj.Win32shutdo wn 12
    > Next
    >
    >
    >
    > but my translation results in "'int' object is not callable" with the
    > attempted call to Win32Shutdown.
    >
    > This is with Python 2.3.2 win32all 1.57 or Active Python 2.3.2
    >
    > print type(obj.Win32S hutDown)
    > gives int
    >
    > print obj.Win32ShutDo wn
    > gives 87 - the ASCII code of "W" - coincidentally ?
    >
    >
    >
    >
    > import win32com.client , sys
    >
    >
    > pc='MyPC'
    >
    > oWMI =
    > win32com.client .GetObject(r"wi nmgmts:{imperso nationLevel=imp ersonate,\
    > (Shutdown)}!\\" + pc + r"\root\cimv 2")
    >
    > colOperatingSys tems = oWMI.ExecQuery( r"Select * from[/color]
    Win32_Operating System")[color=blue]
    >
    > for obj in colOperatingSys tems:
    > obj.Win32ShutDo wn(12)
    >
    >
    >
    >
    > --
    > ___
    > |im ---- ARM Powered ----[/color]


    Comment

    • Tim Howarth

      #3
      Re: win32com (VBScript to Python) problem

      In message <bpve3m$shg$1@n ews-reader4.wanadoo .fr>
      "Rony Steelandt" <bucodi@wanadoo .fr> wrote:
      [color=blue]
      > Not knowing exactly what you want to do, just a question.
      >
      > Why use COM, shutdown is an exe so why not just execute it?[/color]

      Using this method you can shutdown and poweroff (the 12 value) a
      machine, shutdown.exe from the reskit leaves a machine at "You may now
      switch off".


      I want to use this to shutdown all machines in the school in which I
      work from a schedule on a server.

      I could use the VBscript version but there are long delays if a machine
      is switched off - in Python I can thread the communication with
      individual machines (to achieve this in VBScript I have to call a new
      instance of w(c)script for each machine), effectively shutting them
      down in parallel.

      --
      ___
      |im ---- ARM Powered ----

      Comment

      • Michel Claveau/Hamster

        #4
        Re: win32com (VBScript to Python) problem

        Hi !

        This code "reboot" the computer 'CPU01' (if rights are OK) :


        import win32com.client
        WMIService =
        win32com.client .GetObject(r"wi nmgmts:{imperso nationLevel=imp ersonate,(Shutd o
        wn)}!\\CPU01\ro ot\cimv2")

        objs = WMIService.Exec Query(r"Select * from Win32_Operating System")
        for obj in objs:
        obj.Reboot()




        Perhaps this sample can help you ?

        @-salutations
        --
        Michel Claveau
        mél : http://cerbermail.com/?6J1TthIa8B
        site : http://mclaveau.com
        NG : news://news.zoo-logique.org/programmation.Paradox


        Comment

        • Michel Claveau/Hamster

          #5
          Re: win32com (VBScript to Python) problem

          Hi !

          This code "reboot" the computer 'CPU01' (if rights are OK) :


          import win32com.client
          WMIService =
          win32com.client .GetObject(r"wi nmgmts:{imperso nationLevel=imp ersonate,(Shutd o
          wn)}!\\CPU01\ro ot\cimv2")

          objs = WMIService.Exec Query(r"Select * from Win32_Operating System")
          for obj in objs:
          obj.Reboot()




          Perhaps this sample can help you ?

          @-salutations
          --
          Michel Claveau
          mél : http://cerbermail.com/?6J1TthIa8B
          site : http://mclaveau.com
          NG : news://news.zoo-logique.org/programmation.Paradox



          Comment

          • Michel Claveau

            #6
            Re: win32com (VBScript to Python) problem

            Hi !

            This code "reboot" the computer 'CPU01' (if rights are OK) :


            import win32com.client
            WMIService =
            win32com.client .GetObject(r"wi nmgmts:{imperso nationLevel=imp ersonate,(Shutd o
            wn)}!\\CPU01\ro ot\cimv2")

            objs = WMIService.Exec Query(r"Select * from Win32_Operating System")
            for obj in objs:
            obj.Reboot()




            Perhaps this sample can help you ?

            @-salutations
            --
            Michel Claveau
            site : http://mclaveau.com



            Comment

            • Duncan Booth

              #7
              Re: win32com (VBScript to Python) problem

              Tim Howarth <tim@worthy.dem on.co.uk> wrote in
              news:6e24de564c .tim@worthy.dem on.co.uk:
              [color=blue]
              > I want to use this to shutdown all machines in the school in which I
              > work from a schedule on a server.
              >
              > I could use the VBscript version but there are long delays if a machine
              > is switched off - in Python I can thread the communication with
              > individual machines (to achieve this in VBScript I have to call a new
              > instance of w(c)script for each machine), effectively shutting them
              > down in parallel.[/color]

              This isn't a Python solution at all, but have you looked at PsTools
              (http://www.sysinternals.com)? It includes a command to remotely shutdown a
              machine including options to specify whether to log the user off, poweroff,
              reboot, hibernate etc. There is also a timeout so you can give users a
              chance to save their work (it displays a popup on their screen telling them
              the system is shutting down). You could just spawn a bunch of psshutdown
              processes off in parallel. It is free, although you cannot further
              redistribute it.

              PsTools also includes other programs which could be useful in a classroom
              environment (useful for teachers, devastating if the pupils get their hands
              on them and you don't have security set up right).

              --
              Duncan Booth duncan@rcp.co.u k
              int month(char *p){return(1248 64/((p[0]+p[1]-p[2]&0x1f)+1)%12 )["\5\x8\3"
              "\6\7\xb\1\x9\x a\2\0\4"];} // Who said my code was obscure?

              Comment

              • Michel Claveau/Hamster

                #8
                Re: win32com (VBScript to Python) problem

                Sorry for the multi-post, my FAI is too much lazy...



                Comment

                • Tim Howarth

                  #9
                  Re: win32com (VBScript to Python) problem

                  In message <Xns943E7EB585A 78duncanrcpcouk @127.0.0.1>
                  Duncan Booth <duncan@NOSPAMr cp.co.uk> wrote:
                  [color=blue]
                  > Tim Howarth <tim@worthy.dem on.co.uk> wrote in
                  > news:6e24de564c .tim@worthy.dem on.co.uk:
                  >[color=green]
                  > > I want to use this to shutdown all machines in the school in which I
                  > > work from a schedule on a server.[/color]
                  >
                  > This isn't a Python solution at all, but have you looked at PsTools
                  > (http://www.sysinternals.com)?[/color]

                  Yes, very handy tools.

                  It's just that I'd like to do the shutting down myself - why call
                  pshutdown if I can call the shut down mechanism directly.


                  --
                  ___
                  |im ---- ARM Powered ----

                  Comment

                  • Tim Howarth

                    #10
                    Re: win32com (VBScript to Python) problem

                    In message <bpvho4$kon$1@n ews-reader2.wanadoo .fr>
                    "Michel Claveau" <No.Spam.mc@No. Spam.mclaveau.N o.Spam.com> wrote:
                    [color=blue]
                    > import win32com.client
                    > WMIService =
                    > win32com.client .GetObject(r"wi nmgmts:{imperso nationLevel=imp ersonate,(Shutd o
                    > wn)}!\\CPU01\ro ot\cimv2")
                    >
                    > objs = WMIService.Exec Query(r"Select * from Win32_Operating System")
                    > for obj in objs:
                    > obj.Reboot()[/color]



                    This gives me the same error 'int' is not callable.

                    Maybe it's something to do with late/early binding ??

                    What version of Python/win32all have you used this on ?

                    --
                    ___
                    |im ---- ARM Powered ----

                    Comment

                    • Duncan Booth

                      #11
                      Re: win32com (VBScript to Python) problem

                      Tim Howarth <tim@worthy.dem on.co.uk> wrote in
                      news:4700e5564c .tim@worthy.dem on.co.uk:
                      [color=blue]
                      > In message <Xns943E7EB585A 78duncanrcpcouk @127.0.0.1>
                      > Duncan Booth <duncan@NOSPAMr cp.co.uk> wrote:
                      >[color=green]
                      >> Tim Howarth <tim@worthy.dem on.co.uk> wrote in
                      >> news:6e24de564c .tim@worthy.dem on.co.uk:
                      >>[color=darkred]
                      >> > I want to use this to shutdown all machines in the school in which I
                      >> > work from a schedule on a server.[/color]
                      >>
                      >> This isn't a Python solution at all, but have you looked at PsTools
                      >> (http://www.sysinternals.com)?[/color]
                      >
                      > Yes, very handy tools.
                      >
                      > It's just that I'd like to do the shutting down myself - why call
                      > pshutdown if I can call the shut down mechanism directly.
                      >
                      >[/color]
                      I would say because with the pstools version you get a message on screen
                      telling the user that their system is going to shut down in 1 minute (or
                      whatever) and giving them an opportunity to save their work. You might
                      never need that, but it could save someone a lot of grief.


                      --
                      Duncan Booth duncan@rcp.co.u k
                      int month(char *p){return(1248 64/((p[0]+p[1]-p[2]&0x1f)+1)%12 )["\5\x8\3"
                      "\6\7\xb\1\x9\x a\2\0\4"];} // Who said my code was obscure?

                      Comment

                      • Tim Howarth

                        #12
                        Re: win32com (VBScript to Python) problem

                        In message <Xns943E88ECAA0 49duncanrcpcouk @127.0.0.1>
                        Duncan Booth <duncan@NOSPAMr cp.co.uk> wrote:
                        [color=blue][color=green]
                        > > It's just that I'd like to do the shutting down myself - why call
                        > > pshutdown if I can call the shut down mechanism directly.
                        > >
                        > >[/color]
                        > I would say because with the pstools version you get a message on screen
                        > telling the user that their system is going to shut down in 1 minute (or
                        > whatever) and giving them an opportunity to save their work. You might
                        > never need that,[/color]

                        Indeed, for this task I just want to shutdown whatever's left switched
                        on of 300+ PCs, at (say) 10:00pm.


                        --
                        ___
                        |im ---- ARM Powered ----

                        Comment

                        • Matteo Risoldi

                          #13
                          Re: win32com (VBScript to Python) problem

                          Just a comment: is shutdown.exe leaves the machine in the "you can switch
                          off", try shutdown -s, it does the job for me.

                          M.


                          Comment

                          • Tim Howarth

                            #14
                            Re: win32com (VBScript to Python) problem

                            In message <bpvs9j$ovl$1@s unnews.cern.ch>
                            "Matteo Risoldi" <Matteo.Risoldi @cern.ch> wrote:
                            [color=blue]
                            > Just a comment: is shutdown.exe leaves the machine in the "you can switch
                            > off", try shutdown -s, it does the job for me.[/color]

                            Ah, I'm guessing that that is an XP addition, the version of
                            shutdown.exe that I have (from Win2k reskit IIRC) doesn't undertsand
                            that switch.


                            --
                            ___
                            |im ---- ARM Powered ----

                            Comment

                            Working...