How to get Windows user name?

Hi, all

I am trying to detect the user who’s currently login the Windows system.
Is there way to do it in Ruby?

Thanks

Mike J. wrote:

Hi, all

I am trying to detect the user who’s currently login the Windows system.
Is there way to do it in Ruby?

Thanks

require ‘win32ole’
network=WIN32OLE.new(“Wscript.Network”)
puts network.username

#or
puts ENV[‘username’]
#for win95/98 use
puts ENV[‘userid’]

#or (stolen from www.,rubytips.org)
require ‘Win32API’
name = " " * 128
size = “128”
Win32API.new(‘advapi32’,‘GetUserName’,[‘P’,‘P’],‘I’).call(name,size)
puts name.unpack(“A*”)

Take your pick.

Regards,

Siep

On Aug 15, 12:50 am, Mike J. [email protected] wrote:

Hi, all

I am trying to detect the user who’s currently login the Windows system.
Is there way to do it in Ruby?

Thanks

NT/2K/2K3/Vista systems have a environment variable for interactive
users:

irb(main):001:0> ENV[‘USERNAME’]
=> “Luis”

So you only need to asign ENV[‘USERNAME’] and will get the current
username.

If your process is not an interactive one (ala, a Windows service)
then this variable will be nil or the special account used for it
(LocalService).

HTH,

Thanks all for the help. It’s working now. Thanks!

On Aug 14, 4:50 pm, Mike J. [email protected] wrote:

Hi, all

I am trying to detect the user who’s currently login the Windows system.
Is there way to do it in Ruby?

This is the one function of the ‘etc’ library that works on Windows:

require ‘etc’
puts Etc.getlogin

Or, you can use the sys-admin library, which provides much more
functionality on Windows:

gem install sys-admin
require ‘sys/admin’

puts Sys::Admin.get_login

Regards,

Dan

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs