Someone recently asked how to shut down or restart Windows from their Ruby code. Windows provides an executable, "shutdown.exe", to accomplish this. Execute this command from your script with the appropriate parameters and you can log off, shutdown, or restart the OS.
Parameters accepted include, but are not limited to:
-l Log off (cannot be used with -m option)
-s Shutdown the computer
-r Shutdown and restart the computer
-t xx Set timeout for shutdown to xx seconds
-f Forces running applications to close without warning
So, the following line of code will close all applications without a warning and instantly shut down and restart your PC:
system('shutdown.exe -r -f -t 0')
To get the full list of parameters, open a console window and enter
Further details can be found in this Microsoft article. And Gordon Thiesfeld offers WMI and win32ole alternatives in this Ruby Forum thread.
Thanks for stopping by!