[python-win32] rebooting windows from Python?

Tim Golden mail at timgolden.me.uk
Fri Dec 5 11:00:19 CET 2008


Alec Bennett wrote:
>> <code>
>> import wmi
>>
>> wmi.WMI(privileges=["Shutdown"]).Win32_OperatingSystem()[0].Shutdown ()
>>
>> </code>
> 
> Stylin, works. The only thing it doesn't do that I personally need 
> it to do is the "force shutdown". In other words "shutdown.exe -f". 
> I found this page with some hints but couldn't get it to work:
> 
> http://www.freevbcode.com/ShowCode.asp?ID=7693


Well I'm slightly surprised. The MS docs for this method:

  http://msdn.microsoft.com/en-us/library/aa393627(VS.85).aspx

state that there are no params. And, on my XP SP3 box, introspection
says the same:

<code>
>>> import wmi
>>> c = wmi.WMI ()
>>> c.Win32_OperatingSystem.Shutdown
<function Shutdown () => (ReturnValue uint32) | Needs: SeShutdownPrivilege>
>>>

</code>

Ah. I remember. You need the *Win32Shutdown* method, not the *Shutdown*
method. Sorry.

Try this (untested because I can't be fagged to power my machine down
just to test an email :) ):

<code>
import wmi
nForcePowerDown=12 
wmi.WMI(privileges=["Shutdown"]).Win32_OperatingSystem()[0].Win32Shutdown (Flags=nForcePowerDown)
</code>

Earlier versions of the WMI module had a bug/feature which only allowed
named params in method calls. Can't remember whether the current release
version has it fixed. Certainly my dev version does.

TJG


More information about the python-win32 mailing list