psloggedon doesn't do remote machines.
I tried the code below, which works with various levels of success.
WTSUsers.pl seems the way to go, but I have to find all the dependencies (like Win32-Lanman), which shouldn't be a problem.
I'm basically looking for code that I can put into a sub and call it from my code within the same PL file:
$user = loggedon($machine);
Where machine can be an IP or network name.
 
Of course, there are always multiple users logged on, but this will only be used against WinXP Pro workstations, and I only care about the user who is logged in Interactively.
 
Let me know what you think.
Thanks
 
- Adam
 
Code below:
use Win32::OLE qw (in);
use Data::Dumper;
 

my $remotepc = shift; # inserted
 
open (STDOUT, ">c:/tmp/login.log");
 
#Script to find out info on all user sessions on an XP system
 
$debug=0;
 
 
#Create a hashDum to hold references to Login Sessions
my %loginSession = ();
 

#Provide 'word' versions for logontype numbers
 
my @logonTypes = ("None Set",
    "Unknown",
    "Interactive",
    "Network",
    "Batch",
    "Proxy",
    "Unlock",
    "NetworkClearText",
    "NewCredentials",
    "RemoteInteractive",
    "CachedCredentials",
    "CachedRemoteInteractive",
    "CachedUnlock");
 
my $wmi;
if ($remotepc eq "") {
 $wmi= Win32::OLE->GetObject("winmgmts:")
    or die "wmi connect failed!";
} else {
 $wmi= Win32::OLE->GetObject("winmgmts://$remotepc/")
    or die "wmi connect failed!";
}
 

#my $wmi = Win32::OLE->GetObject("winmgmts:")
#    or die "wmi connect failed!";
 
my $sessions = $wmi->ExecQuery("SELECT * FROM Win32_LogonSession")
    or die "Win32_LoginSession connect failed!";
 
my $users = $wmi->ExecQuery("SELECT * FROM Win32_LoggedOnUser")
    or die "Win32_LoggedOnUser connect failed!";
 

foreach my $session(in $sessions) {
   
#Create 'short' names for the Available Properties
    my $id = $session->LogonId;
    my $type = $session->LogonType;
    my $wordtype = $logonTypes[$type];
    my $startTime = $session->StartTime;
   
#Cut the milliseconds off the startTime value
    ($startTime,undef) = split(/\./, $startTime);
   
   
    print "ID: $id\n" if ($debug);
    print "Type: $type\n" if ($debug);
    print "WordType: $wordtype\n" if ($debug);
    print "Time: $startTime\n" if ($debug);
   
#Create a temporary hash, populate it from the $session property
    my %temp = ();
 
#"id" is obviously optional in this hash, since that is also its name :)
    %temp = ("id" => "$id",
      "numtype" => "$type",
      "wordtype" => "$wordtype",
      "time" => "$startTime"
      );
   
#create a reference to the temp hash in %loginSession
    $loginSession{$id} = \%temp;
   
}
 

print Dumper(\%loginSession) if ($debug);
 

#Now repeat the above process for the LoggedOnUser object
 
foreach my $user(in $users) {
 
#Create 'short names' for the objects properties
    my $ant = $user->Antecedent;
    my $dep = $user->Dependent;
 
    print "Ant: $ant\n" if ($debug);
    print "Dep: $dep\n" if ($debug);
 
    #Cut out the rest of the line, and inverted commas, for both properties
    my $id = (split(/LogonId=/, $dep))[1];
    $id =~ s/\"//g;
   
    my $name = (split(/Name=/, $ant))[1];
    $name =~ s/\"//g;
 
    print "ID: $id\n" if ($debug);
    print "Name: $name\n" if ($debug);
 
#Append it to the referenced hash matching the id, held in %loginSession
    $loginSession{$id}->{"name"} = "$name";
 
   
}
 
print Dumper(\%loginSession) if ($debug);
 
#Print out a quick summary of each Login Session
 
print "-----------------------------------\n";
print "ID\tUser\tType\tStartTime\n";
print "-----------------------------------\n";
foreach my $session(keys (%loginSession)) {
   
    print $session .
 "\t" .
 $loginSession{$session}->{"name"} .
 "\t" .
 $loginSession{$session}->{"numtype"} . ":".
 $loginSession{$session}->{"wordtype"} .
 "\t" .
 $loginSession{$session}->{"time"} .
 "\n";
 
   
}
 
 


From: Thomson Steven R Contr AFRL/VSIO [mailto:[EMAIL PROTECTED]
Sent: Thursday, October 13, 2005 6:24 PM
To: Adam Stern; Perl-Win32-Admin@listserv.ActiveState.com
Subject: RE: Remote User

You can parse the output of Sysinternals tool psloggedon.
 

Steven Thomson
Unisys
AFRL E-Mail and Lan Support
505-853-3148
[EMAIL PROTECTED]
"Before they invented drawing boards, what did they go back to?"
 

 


From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Adam Stern
Sent: Tuesday, October 11, 2005 7:47 PM
To: Perl-Win32-Admin@listserv.ActiveState.com
Subject: Remote User

Is there a way to tell who is currently logged into another workstation on the network via perl?

Thanks!

 

- Adam

_______________________________________________
Perl-Win32-Admin mailing list
Perl-Win32-Admin@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to