Forum: Ruby Accessing Windows actions(Shutdow.Restart)

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Da22709962fc0cf31068e779c78ab6be?d=identicon&s=25 Raveendran .P (jazzezravi)
on 2008-04-21 08:25
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
Bfa670ef2303deb7dec5a8027367b30b?d=identicon&s=25 David Mullet (mully)
on 2008-04-21 14:38
Raveendran Jazzez 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

One method is to run the command "shutdown.exe" with parameters:

  system('shutdown.exe -r -f -t 0')

To get a list of parameters, open a console window and enter

  shutdown.exe /?

David

http://rubyonwindows.blogspot.com
Ec07fbda510a142f5331ffd903b07820?d=identicon&s=25 Gordon Thiesfeld (Guest)
on 2008-04-21 15:50
(Received via mailing list)
On Mon, Apr 21, 2008 at 1:25 AM, Raveendran Perumalsamy
<jazzezravi@gmail.com> wrote:
>  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
Da22709962fc0cf31068e779c78ab6be?d=identicon&s=25 Raveendran .P (jazzezravi)
on 2008-04-22 06:57
Attachment: shutdown.JPG (70 KB)
Thank for David and Gardon.

Hi Gardon,

Very good code from u. But i have some trouble when i change LogOff to
Shutdown. I herewith attached the file shows my problem to u.


Awaiting your reply

Regards,
 P.Raveendran
 RailsFactory,Chennai.
 http://raveendran.wordpress.com
Ec07fbda510a142f5331ffd903b07820?d=identicon&s=25 Gordon Thiesfeld (Guest)
on 2008-04-22 14:36
(Received via mailing list)
On Mon, Apr 21, 2008 at 11:57 PM, Raveendran Jazzez
<jazzezravi@gmail.com> wrote:
> Thank for David and Gardon.
>
>  Hi Gardon,
>
>  Very good code from u. But i have some trouble when i change LogOff to
>  Shutdown. I herewith attached the file shows my problem to u.
>

Ok, you'll need the 'Shutdown' privilege for a local machine and the
RemoteShutdown privilege for a remote machine.

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

require 'rubygems'
require 'ruby-wmi'


os = WMI::Win32_OperatingSystem.find( :first, :privileges =>
[WMI::Privilege::Shutdown] )
os.win32shutdown( Shutdown )

Some links:

http://ruby-wmi.rubyforge.org/doc/classes/WMI/Privilege.html
http://msdn2.microsoft.com/en-us/library/aa394058(...
http://msdn2.microsoft.com/en-us/library/aa392758.aspx

hope that helps,

Gordon
Da22709962fc0cf31068e779c78ab6be?d=identicon&s=25 Raveendran .P (jazzezravi)
on 2008-05-03 11:57
Hi All,

I got simple solution. But need t sharp it.

I got code to shutdown my machine, getting ipconfig in my machine
etc..,(whatever possible to get via command prompt.)

My Code:

system('ipconfig')

I got my IP Address.

Problem:

a= system('ipconfig')

puts a

I got only true. Please help to store the content in single variable.

Regards,
 P.Raveendran
 RailsFactory,Chennai.
 http://raveendran.wordpress.com
8fa168c01383efb3e9b72a2afd1c3384?d=identicon&s=25 L. G. (dezertir)
on 2008-05-03 13:55
Raveendran Jazzez wrote:
> My Code:
>
> system('ipconfig')
>
> I got my IP Address.
>
> Problem:
>
> a= system('ipconfig')
>
> puts a
>
> I got only true. Please help to store the content in single variable.

Hi..
I tried your code and all OK!

Try this:

def myIpConfig
 system('ipconfig') // or puts system('ipconfig')
end

myIpConfig // print your ip config
Da22709962fc0cf31068e779c78ab6be?d=identicon&s=25 Raveendran .P (jazzezravi)
on 2008-05-03 14:02
> Hi..
> I tried your code and all OK!
>
> Try this:
>
> def myIpConfig
>  system('ipconfig') // or puts system('ipconfig')
> end
>
> myIpConfig // print your ip config

Hi Luka,

 i did this already. But need to get in a single variable. I think it is
not possible. Anyway thanks a lot ..


Regards,
 P.Raveendran
 RailsFactory,Chennai.
 http://raveendran.wordpress.com
666b4e17b4bb0e2d999037a25f65a7cb?d=identicon&s=25 Heesob Park (phasis)
on 2008-05-03 14:07
Hi,

Raveendran Jazzez wrote:
> Hi All,
>
> I got simple solution. But need t sharp it.
>
> I got code to shutdown my machine, getting ipconfig in my machine
> etc..,(whatever possible to get via command prompt.)
>
> My Code:
>
> system('ipconfig')
>
> I got my IP Address.
>
> Problem:
>
> a= system('ipconfig')
>
> puts a
>
> I got only true. Please help to store the content in single variable.
>
Try with backtick operator:
a = `ipconfig`

If you want only IP Address, you can do something like this:
a = `ipconfig`.scan(/IP Address.+?: (.+)\r/).to_s

Regards,
Park Heesob
Ec07fbda510a142f5331ffd903b07820?d=identicon&s=25 Gordon Thiesfeld (Guest)
on 2008-05-03 14:09
(Received via mailing list)
On Sat, May 3, 2008 at 7:02 AM, Raveendran Jazzez <jazzezravi@gmail.com>
wrote:
>   i did this already. But need to get in a single variable. I think it is
>  not possible. Anyway thanks a lot ..
>

Use backticks:

>> a = `ipconfig`
=> "\r\nWindows IP Configuration\r\n\r\n\r\nEthernet adapter Wireless
Network Connection 2:\r\n\r\n        Connection-specific DNS Suffix  .
: \r\n        IP Address. . . . . . . . . . . . : 192.168.1.101\r\n
    Subnet Mask . . . . . .
. . . . . : 255.255.255.0\r\n        Default Gateway . . . . . . . . .
: 192.168.1.1\r\n"
>> a
=> "\r\nWindows IP Configuration\r\n\r\n\r\nEthernet adapter Wireless
Network Connection 2:\r\n\r\n        Connection-specific DNS Suffix  .
: \r\n        IP Address. . . . . . . . . . . . : 192.168.1.101\r\n
    Subnet Mask . . . . . .
. . . . . : 255.255.255.0\r\n        Default Gateway . . . . . . . . .
: 192.168.1.1\r\n"
4828d528e2e46f7c8160c336eb332836?d=identicon&s=25 Robert Heiler (shevegen)
on 2008-05-03 14:10
> def myIpConfig
>  system('ipconfig') // or puts system('ipconfig')
> end
> myIpConfig // print your ip config



> i did this already. But need to get in a single variable.


Solution:
  a = `ipconfig`
8fa168c01383efb3e9b72a2afd1c3384?d=identicon&s=25 L. G. (dezertir)
on 2008-05-03 14:12
Raveendran Jazzez wrote:
> Hi Luka,
>
>  i did this already. But need to get in a single variable. I think it is
> not possible. Anyway thanks a lot ..
>
>
> Regards,
>  P.Raveendran
>  RailsFactory,Chennai.
>  http://raveendran.wordpress.com

No no, it's possible, i tried:
  var = system('ipconfig')
  puts var
and all is fine.

try this:
  var = "#{system('ipconfig')}"
  puts "#{var}"

I think it must work.
Da22709962fc0cf31068e779c78ab6be?d=identicon&s=25 Raveendran .P (jazzezravi)
on 2008-05-03 14:13
> Try with backtick operator:
> a = `ipconfig`
>
> If you want only IP Address, you can do something like this:
> a = `ipconfig`.scan(/IP Address.+?: (.+)\r/).to_s
>
> Regards,
> Park Heesob

Hi Park,

Fantastic idea from u. Thank a lot.

Hi All,

We will continue with more problems very soon.....

Regards,
 P.Raveendran
 RailsFactory,Chennai.
 http://raveendran.wordpress.com
Da22709962fc0cf31068e779c78ab6be?d=identicon&s=25 Raveendran .P (jazzezravi)
on 2008-05-03 14:18
Hi All,

Previously i am used 'ipconfig' --> It should be `ipconfig`

So All are eager to fix more issues from me.. k let us see


 Regards,
  P.Raveendran
  RailsFactory,Chennai.
  http://raveendran.wordpress.com
This topic is locked and can not be replied to.