Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Getting windows account name

by TonyM (Initiate)
on Apr 18, 2007 at 08:56 UTC ( [id://610728]=perlquestion: print w/replies, xml ) Need Help??

TonyM has asked for the wisdom of the Perl Monks concerning the following question:

Hi, Is there a way to get the Windows Account name that is in use when running a perl script in windows? I havnt much experience in running perl under windows but i want to create a logfile that logs the user that ran the script. thanks in advance

Replies are listed 'Best First'.
Re: Getting windows account name
by marto (Cardinal) on Apr 18, 2007 at 09:26 UTC
Re: Getting windows account name
by pKai (Priest) on Apr 18, 2007 at 13:14 UTC
    Assuming you are on an ActiveState Perl:
    use strict; use warnings; #use Win32; print 'I am ', Win32::LoginName, ' in domain ', Win32::DomainName, $/;

    In addition to the informative thread marto referred to above, which points to Win32::LoginName, knowing the domain an accounts comes from might be of interest too, especially in a bigger AD environment (forest).

    Also note, that ActiveState's perl on Windows is special, with regard to the Win32 module, insofar as it distinguishes between "[CORE]" functions and others.

    Those [CORE] functions are available, even without a "use Win32;" (LoginName and DomainName among them).

    More information on Win32 functions is available from the cmd command line with perldoc Win32.

Re: Getting windows account name
by roboticus (Chancellor) on Apr 18, 2007 at 12:09 UTC
    TonyM:

    You might try this...

    print "User=$ENV{USER}, UserName=$ENV{USERNAME}\n";
    ...roboticus

    Update: Uppercased 'user'

Re: Getting windows account name
by Moron (Curate) on Apr 18, 2007 at 12:26 UTC
    ^B Hmmm, no-one mentioned it and I did a supersearch too see what the OP would find and the even the Perl docs (perlvar) doesn't make it clear enough (not even close), blah, moan, grizzle, etc. but...

    ^C Doesn't the $< builtin variable work on windows then?

    (I can't test it myself because this PC was put in a some kind of Windows jail by a troupe of trained monkeys working for the Thought Police - Hummingbird and IE are my only escape routes).

    Update: having now tested on unix, $< produces the uid numerically so ok that sort of answers my own question.

    __________________________________________________________________________________

    ^M Free your mind!

    Key to hats: ^I=white ^B=black ^P=yellow ^E=red ^C=green ^M=blue - see Moron's scratchpad for fuller explanation.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://610728]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2024-03-28 12:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found