Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to determine what user is logged locally into Windows XP/2K?

Status
Not open for further replies.

TheNetGuy

IS-IT--Management
Joined
Sep 14, 2003
Messages
2
Location
US
I am running into a problem where a perl script called by the Schedule service in Windows XP is not determining the real locally logged in username. Instead of returning the current username that is logged in, it returns "SYSTEM" which is the account used by Schedule service.

Below are few ways that I tried to identify what username is locally logged into computer console:

1. Environment Variable

$USERID = $ENV{'USERNAME');

Returns no value when the perl script is called by the
scheduler running as SYSTEM.

2. Win32 Function calls

$USERID = Win32::AdminMisc::GetLogonName(); (OR)
$USERID = Win32::LoginName();

Both GetLogonName() and LoginName() return "SYSTEM",
which is the owner of process running the perl script
but not the actual logged in user. By the way, if I
execute the perl script directly and not through the
Scheduler service, it returns the real user name in
all three case show above.

3. Win32::NetAdmin::LoggedOnUsers()

This function is almost perfect, but it has two flaws.

a) It returns accounts that have logged off from
console.

PJaques (Logged off)
MJaques (Logged off)
Administrator (Current Local logged on User)
IWAM_MYCOMPUTER (Used by web server)

b) If a user logs out from computer, LoggedOnUsers()
doesn't indicate that nobody is logged locally
into the computer console.

It seems like there should be an easy way to determine what single user is logged in locally to the computer's console. I just haven't been able to figure out how. Any help would be appreciated. Please provide example. Thanks.

-- Patrick
 
That's not what I get - I get a hash with:

1 Michael.Lacey;DOMAIN_NAME;SERVER_NAME
0 MIKE-LACEY$;DOMAIN_NAME;

I'm running this on XP

What exactly do you get?

Mike

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

It's like this; even samurai have teddy bears, and even teddy bears get drunk.
 
I was thinking that it should be easy to loop through whatever's returned by that function and look for the string "Current Local" to find the user you're after.

Mike

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

It's like this; even samurai have teddy bears, and even teddy bears get drunk.
 
Hi Mike,

When I call Win32::NetAdmin::LoggedOnUsers($server,\%hash) on Windows XP, I seems to work properly. Although, I do not see a '1' or '0' in the hash. When I call the same function on a Windows 2000 Pro PC, it doesn't clear out logged out users. For example, I logged in as PJaques, then logged out, logged in as Administrator, then logged out, then logged back in as PJaques, which produced the below results:

(Windows 2000)
------------
PJaques;SNOOPY;SNOOPY
Administrator;SNOOPY;SNOOPY
PJaques;SNOOPY;SNOOPY

Note: Administrator should not show up in the hash, since it is no longer logged in.

(Windows XP)
----------
PJaques;PJAQUES-XP;PJAQUES-XP
PJAQUES-XP$;HOME
PJaques;PJAQUES-XP;PJAQUES-XP

As to the hash, I only see user;ldomain;server, not a leading 1 or 0. Below is the code I am using. Am I doing coding something different than you?
----------------------------------------------------
#!Perl

use strict;
use Win32::NetAdmin qw(LoggedOnUsers);

my %users;
my $server = "";
my $stat = 0;

$stat = LoggedOnUsers( $server, \%users);

print "Users logged on to $server:\n\n";
printf ("User\t\tDomain\t\tServer\n\n");
for (keys %users) {
my ($user, $udomain, $userver) = split /;/, $users{$_};
printf ("%-15s %-15s %-15s\n", $user,$udomain,$userver);
}
--------------------------------------------------------

--Patrick
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top