[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Accessing Windows actions(Shutdow.Restart

Raveendran Jazzez

4/21/2008 6:25:00 AM

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.wor...
--
Posted via http://www.ruby-....

13 Answers

David Mullet

4/21/2008 12:38:00 PM

0

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.wor...

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.bl...
--
Posted via http://www.ruby-....

Gordon Thiesfeld

4/21/2008 1:50:00 PM

0

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.wor...
> --
> Posted via http://www.ruby-....
>
>

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

Raveendran Jazzez

4/22/2008 4:58:00 AM

0

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.wor...

Attachments:
http://www.ruby-...attachment/1766/sh...

--
Posted via http://www.ruby-....

Gordon Thiesfeld

4/22/2008 12:35:00 PM

0

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/Priv...
http://msdn2.microsoft.com/en-us/librar...(VS.85).aspx
http://msdn2.microsoft.com/en-us/library/aa3...

hope that helps,

Gordon

Raveendran Jazzez

5/3/2008 9:57:00 AM

0

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.wor...

--
Posted via http://www.ruby-....

luka luka

5/3/2008 11:55:00 AM

0

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
--
Posted via http://www.ruby-....

Raveendran Jazzez

5/3/2008 12:02:00 PM

0

> 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.wor...

--
Posted via http://www.ruby-....

Heesob Park

5/3/2008 12:07:00 PM

0

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
--
Posted via http://www.ruby-....

Gordon Thiesfeld

5/3/2008 12:09:00 PM

0

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"
>>

Marc Heiler

5/3/2008 12:11:00 PM

0

> 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`
--
Posted via http://www.ruby-....