win32com (VBScript to Python) problem

T

Tim Howarth

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)
 
R

Rony Steelandt

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
 
T

Tim Howarth

In message <[email protected]>
"Rony Steelandt said:
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.
 
M

Michel Claveau/Hamster

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
 
M

Michel Claveau/Hamster

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
 
M

Michel Claveau

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
 
D

Duncan Booth

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

Tim Howarth

In message <[email protected]>
"Michel Claveau said:
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 ?
 
D

Duncan Booth

In message <[email protected]>


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

Tim Howarth

In message <[email protected]>
Duncan Booth said:
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.
 
M

Matteo Risoldi

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

Tim Howarth

In message <[email protected]>
"Matteo Risoldi said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top