3

I need get the name of the domain user in Windows.

Here are the ways by which I tried to get a name, but they all return the domain name:

1.

...
ENV['username'] # or ENV['USERNAME'] or ENV['userid']
...

2.

...
require 'etc'
puts Etc.getlogin
...

3.

...
require 'dl/win32'

def get_user_name
  api = Win32API.new(
    'advapi32.dll',
    'GetUserName',
    'PP',
    'i'
  )

  buf = "\0" * 512
  len = [512].pack('L')
  api.call(buf,len)

  buf[0..(len.unpack('L')[0])]
end
...

4.

This way return string "system":

...
require 'win32ole'
network=WIN32OLE.new("Wscript.Network")
puts network.username
... 

How can I get username or pair domain\username?

tripleee
  • 175,061
  • 34
  • 275
  • 318

1 Answers1

0

Can't you run system commands and store them in a variable.

u = `WhoAmI /user` 

Now u has the user information for your processing, isnt?

Bala
  • 11,068
  • 19
  • 67
  • 120