Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

win32com (VBScript to Python) problem

0 views
Skip to first unread message

Tim Howarth

unread,
Nov 25, 2003, 6:16:25 AM11/25/03
to

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:{impersonationLevel=impersonate,(Shutdown)}!\\"_
& pc & "\root\cimv2")

Set colOperatingSystems = oWMI.ExecQuery("Select * from Win32_OperatingSystem")

For Each obj in colOperatingSystems
obj.Win32shutdown 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.Win32ShutDown)
gives int

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


import win32com.client, sys


pc='MyPC'

oWMI =
win32com.client.GetObject(r"winmgmts:{impersonationLevel=impersonate,\
(Shutdown)}!\\" + pc + r"\root\cimv2")

colOperatingSystems = oWMI.ExecQuery(r"Select * from Win32_OperatingSystem")

for obj in colOperatingSystems:
obj.Win32ShutDown(12)


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

Rony Steelandt

unread,
Nov 25, 2003, 6:27:36 AM11/25/03
to
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" <t...@worthy.demon.co.uk> a écrit dans le message de news:
d715dc5...@worthy.demon.co.uk...

Tim Howarth

unread,
Nov 25, 2003, 6:38:53 AM11/25/03
to
In message <bpve3m$shg$1...@news-reader4.wanadoo.fr>
"Rony Steelandt" <buc...@wanadoo.fr> wrote:

> Not knowing exactly what you want to do, just a question.
>
> Why use COM, shutdown is an exe so why not just execute it?

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.

Michel Claveau/Hamster

unread,
Nov 25, 2003, 7:20:40 AM11/25/03
to
Hi !

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


import win32com.client
WMIService =
win32com.client.GetObject(r"winmgmts:{impersonationLevel=impersonate,(Shutdo
wn)}!\\CPU01\root\cimv2")

objs = WMIService.ExecQuery(r"Select * from Win32_OperatingSystem")
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


Michel Claveau/Hamster

unread,
Nov 25, 2003, 7:23:10 AM11/25/03
to
Hi !

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


import win32com.client
WMIService =
win32com.client.GetObject(r"winmgmts:{impersonationLevel=impersonate,(Shutdo

Michel Claveau

unread,
Nov 25, 2003, 7:28:29 AM11/25/03
to
Hi !

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


import win32com.client
WMIService =
win32com.client.GetObject(r"winmgmts:{impersonationLevel=impersonate,(Shutdo


wn)}!\\CPU01\root\cimv2")

objs = WMIService.ExecQuery(r"Select * from Win32_OperatingSystem")
for obj in objs:
obj.Reboot()


Perhaps this sample can help you ?

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

Duncan Booth

unread,
Nov 25, 2003, 7:35:42 AM11/25/03
to
Tim Howarth <t...@worthy.demon.co.uk> wrote in
news:6e24de5...@worthy.demon.co.uk:

> 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.

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 dun...@rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?

Michel Claveau/Hamster

unread,
Nov 25, 2003, 7:45:38 AM11/25/03
to
Sorry for the multi-post, my FAI is too much lazy...

Tim Howarth

unread,
Nov 25, 2003, 7:53:48 AM11/25/03
to
In message <Xns943E7EB585A...@127.0.0.1>
Duncan Booth <dun...@NOSPAMrcp.co.uk> wrote:

> Tim Howarth <t...@worthy.demon.co.uk> wrote in
> news:6e24de5...@worthy.demon.co.uk:
>
> > I want to use this to shutdown all machines in the school in which I
> > work from a schedule on a server.
>

> This isn't a Python solution at all, but have you looked at PsTools
> (http://www.sysinternals.com)?

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.

Tim Howarth

unread,
Nov 25, 2003, 7:58:43 AM11/25/03
to
In message <bpvho4$kon$1...@news-reader2.wanadoo.fr>
"Michel Claveau" <No.Sp...@No.Spam.mclaveau.No.Spam.com> wrote:

> import win32com.client
> WMIService =
> win32com.client.GetObject(r"winmgmts:{impersonationLevel=impersonate,(Shutdo
> wn)}!\\CPU01\root\cimv2")
>
> objs = WMIService.ExecQuery(r"Select * from Win32_OperatingSystem")
> for obj in objs:
> obj.Reboot()

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 ?

Tim Golden

unread,
Nov 25, 2003, 7:40:57 AM11/25/03
to Tim Howarth, pytho...@python.org
> 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;

[... snip VB script culminating in obj.Win32shutdown 12 ...]

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

[... snip Python translation of same ...]

I'm not going to attempt to describe what you need to do to send WMI
methods and parameters back and forth, because it's a pain in the neck,
which is why I hid all the hard work under the covers here:

http://tgolden.sc.sabren.com/python/wmi.html

and can even offer a comparable example here:

http://tgolden.sc.sabren.com/python/wmi_cookbook.html
(search for "Reboot a remote machine" -- I really must put anchors on that
page)

If you follow that example, all you need to change is the final line
to read something like:

os.Win32Shutdown (Flags=12)

and it works! (Well, it did for me, anyway).

HTH
Tim

PS If you read the source of the module you'll understand why I
didn't want to try to explain how it works. Feel free to tinker
and offer improvements. TJG


________________________________________________________________________
This e-mail has been scanned for all viruses by Star Internet. 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 Howarth

unread,
Nov 25, 2003, 8:23:01 AM11/25/03
to
In message <mailman.1066.106976...@python.org>
Tim Golden <tim.g...@viacom-outdoor.co.uk> wrote:

> http://tgolden.sc.sabren.com/python/wmi_cookbook.html
> (search for "Reboot a remote machine" -- I really must put anchors on that
> page)
>
> If you follow that example, all you need to change is the final line
> to read something like:
>
> os.Win32Shutdown (Flags=12)

Thanks to Tim, that works, though I still wonder why I can't do it
my original way.

Duncan Booth

unread,
Nov 25, 2003, 8:33:27 AM11/25/03
to
Tim Howarth <t...@worthy.demon.co.uk> wrote in
news:4700e55...@worthy.demon.co.uk:

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.

Tim Howarth

unread,
Nov 25, 2003, 9:33:49 AM11/25/03
to
In message <Xns943E88ECAA0...@127.0.0.1>
Duncan Booth <dun...@NOSPAMrcp.co.uk> wrote:

> > 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.
> >
> >
> 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,

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

Tim Golden

unread,
Nov 25, 2003, 10:18:33 AM11/25/03
to pytho...@python.org
> > http://tgolden.sc.sabren.com/python/wmi_cookbook.html
> > (search for "Reboot a remote machine" -- I really must put
> anchors on that
> > page)
> >
> > If you follow that example, all you need to change is the final line
> > to read something like:
> >
> > os.Win32Shutdown (Flags=12)
>
> Thanks to Tim, that works, though I still wonder why I can't do it
> my original way.

For the purposes of elucidation, I offer the following
half-baked explanation, based on my re-reading my own
code:

For reasons unknown to me, you don't simply call an arbitrary
named method on a WMI object; rather, you call the ExecMethod_
method of the object, having passed parameters in, which must
themselves have been set up via Methods_ ().InParameters. The
result and any outward parameters are picked up via the
OutParameters property. (That was a bit simplified).

Obviously, given the win32com module you can do all this by
yourself. When all's said and done, that's exactly what my
module is doing. However, it's as tedious as anything. So...
the wmi module sets up a combination of __getattr__ hooks and
currying classes to take the burden away from you.

If you're interested enough, the relevant -- underdocumented -- code
is in the _wmi_method class of the wmi module, hooked into from
the __getattr__ method of the _wmi_object class which tries
to work out whether you're referring to a method (in which case it
sets up a proxy instance which is then called) or a property (in
which case it returns the .Value property of the named property).

There is a __setattr__ counterpart to all this, but as has been
pointed out several times recently on this list, WMI is given
more to reading than to writing.

Well... you did ask! I would add, finally, that putting this code
together some six months ago now, was both an education and a
valuable experience, not simply in learning about WMI, but also
in just how much information is available in the c.l.py archives
if you look hard enough. I raise my hat to all those who have
posted working code and explanations over the years.

Matteo Risoldi

unread,
Nov 25, 2003, 10:25:37 AM11/25/03
to
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.


Tim Howarth

unread,
Nov 25, 2003, 12:07:21 PM11/25/03
to
In message <bpvs9j$ovl$1...@sunnews.cern.ch>
"Matteo Risoldi" <Matteo....@cern.ch> wrote:

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

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.

Brian Quinlan

unread,
Nov 25, 2003, 3:20:08 PM11/25/03
to pytho...@python.org
What is pyMobile?

pyMobile is a pure-Python library that allows you to communicate with a
mobile phone through a serial connection (e.g. through Bluetooth).

Currently the most interesting feature of pyMobile is the ability to send
SMS messages.

pyMobile requires the following Python libraries:

pySerial - http://pyserial.sourceforge.net/
Optik - http://optik.sourceforge.net/
(for Python versions 2.2.x and earlier)

The pyMobile website is at:
http://pymobile.sourceforge.net

You can download pyMobile from:
https://sourceforge.net/project/showfiles.php?group_id=93384

Cheers,
Brian


0 new messages