Rebooting a Machine
Occasionally, it’s necessary to force a Windows NT computer to reboot programmatically. You may need to perform a scheduled reboot of the current machine or force a reboot of a remote PC programmatically.
The function
win32api.InitiateSystemShutdown()
appears perfect for the job.
win32api.InitiateSystemShutdown(machine,message,timeout,bForce,bReboot)
-
machine The name of the machine to shutdown or
Nonefor the current machine.-
message A message to be displayed to the user in a dialog while the
timeoutperiod expires.-
timeout A timeout in seconds, during which time a dialog is displayed warning the user of the pending shutdown. After the timeout expires, the shutdown process begins. If this is zero, the shutdown commences immediately.
-
bForce Specifies whether applications with unsaved changes are to be forcibly closed. If this parameter is
true, such applications are closed. If this parameter isfalse, a dialog box is displayed prompting the user to close the applications. Note that this implies the user could cancel the shutdown process by selecting Cancel in the dialog his application displays for unsaved data.-
bReboot Specifies whether the machine is rebooted after the shutdown process.
Let’s try this function. Start by rebooting the current machine with a 30-second timeout (without forcing applications shut) and finally restart after shutdown:
>>> import win32api >>> message = "This machine is being rebooted because it has been naughty" >>> win32api.InitiateSystemShutdown(None, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access