On Mon, Apr 21, 2008 at 1:25 AM, Raveendran Perumalsamy
<jazzezravi / gmail.com> wrote:
> Hi All,
>
>  I want to access the windows functionalities such as Shut down, Restart,
>  Log off etc..
>
>  Please guide to me..
>
>  Because i tried win32OLE, Watir and few gems.. But still there is no
>  improvement to access those things.
>
>  Awaiting reply.
>
>  Regards,
>  P.Raveendran
>  RailsFactory,Chennai.
>  http://raveendran.wordpress.com
>  --
>  Posted via http://www.ruby-forum.com/.
>
>

Here are a few ways.  You'll need admin privileges on any box you run
it against.

Using WMI via win32ole locally.
<code>

LogOff         = 0
ForcedLogOff   = 4
Shutdown       = 1
ForcedShutdown = 5
Reboot         = 2
ForcedReboot   = 6
PowerOff       = 8
ForcedPowerOff = 12

require 'win32ole'

wmi = WIN32OLE.connect("winmgmts:\\\\.\\root\\cimv2")
list = wmi.execquery("Select * from Win32_OperatingSystem",nil,48)

list.each do |item|
  item.win32Shutdown(LogOff)
end
</code>

Using ruby-wmi (gem install ruby-wmi) locally.
<code>

require 'ruby-wmi'
#you'll need the same constants as above

host = 'computername'
os    = WMI::Win32_OperatingSystem.find(:first)
os.win32shutdown(LogOff )

</code>


Using win32ole remotely.
<code>
require 'win32ole'
# constants
computer_name = 'host'
wmi = WIN32OLE.connect("winmgmts:\\\\#{computer_name}\\root\\cimv2")
list = wmi.execquery("Select * from Win32_OperatingSystem",nil,48)

list.each do |item|
  item.win32Shutdown(LogOff )
end

</code>

Using ruby-wmi remotely.
<code>

require 'ruby-wmi'
# constants
host = 'computername'
os    = WMI::Win32_OperatingSystem.find(:first, :host => host)
os.win32shutdown(LogOff )

</code>

Gordon