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

I am a new perl programmer and I am trying to write a script that will (sitting on my domain controller) read in a text file that has a list of computer names. This file will be in the same directory as my script. I would like the script to check each computer, see if a user is logged on or not, and append the findings to a text file. If a user is logged on and write the computername and user's ID in this file. If no one is logged on the computer, it would just write the computername with a message like "No user logged on." Would you be able to point me in the right direction? I looked for examples on the web site but couldn't find anything. I am so new to writing perl I might have over looked something. Thanks for any assistance anyone could share. Devon22

Replies are listed 'Best First'.
Re: Script to capture logged on users
by davis (Vicar) on Aug 26, 2005 at 15:51 UTC
    'cos I had it sitting around:
    use warnings; use strict; use Win32::NetAdmin qw(GetUsers GetServers GetDomainController LoggedO +nUsers UserGetAttributes UserSetAttributes FILTER_NORMAL_ACCOUNT SV_T +YPE_ALL ); use Fcntl; my $domain; my $pdc; ($domain = Win32::DomainName) or die "Unable to obtain the domain name"; Win32::NetAdmin::GetDomainController("", $domain, $pdc) or die "Unable to obtain the PDC name for $domain."; print "Get Domain Controller returned: $pdc\n"; my %machines; GetServers("", $domain, SV_TYPE_ALL, \%machines) or die "GetServers failed: $!\n"; print "\n\n===Machine to User Mapping\n"; my %users_machine; foreach my $machine (sort keys(%machines)) { my @users; unless(LoggedOnUsers($machine, \@users)) { warn "LoggedOnUsers for machine $machine failed: $!\n" +; next; } my %unique_users; $unique_users{$_}++ for @users; foreach (sort keys(%unique_users)) { unless($_ eq $machine."\$") { print "$machine: $_\n"; push @{ $users_machine{$_} }, $machine; my $dbmkey = $machine . ":" . $_; } } } print "\n\n===User to Machine Mapping\n"; print "$_ => ", join(",", @{$users_machine{$_}}), "\n" for (sort keys( +%users_machine));

    davis
    Kids, you tried your hardest, and you failed miserably. The lesson is: Never try.
      This is far more advnced than I'm used to! Where would I specify a file to get the computer names to check? I wouldn't want the script to check every box on the network. The script will be on the domain controller, so would that make a difference? Where in the file would I put in a line to pipe the information to a text file? In my flat file (computers.txt, this wiull hold my list of computers that I want to be checked. The information that I would be looking for is "computer name" and "user ID", both of which would be piped into "results.txt". If there isn't a user logged into a computer, it would reflect in the file results.txt "computer name" and "no logged in user". This might do most of this but, again I'm not sure! Sorry for being so stupid about perl and new to programming.
        If you're new to programming, I suggest writing your algorithm out in psuedocode first. Your requirements might look like this:
        open INFILE for reading open RESULTS for writing foreach machine_name (in INFILE) { # loop over lines in input file results = LoggedOnUsers(machine_name) print results to RESULTS } close INFILE close RESULTS
        That's the basis of your algorithm (without any error-checking — you'd want to add this). I'd look at the following for the syntax:
        perldoc -f open perldoc perlsyn # For the "foreach" loop perldoc Win32::NetAdmin perldoc -f print perldoc perlintro # May be of use
        I'm pretty sure that it doesn't matter which machine you run this from. I'm also pretty sure that you'd have to run this as a Domain Admin (to have rights to the LoggedOnUsers function)
        Welcome to the world of scholastic monastics, btw.

        davis
        Kids, you tried your hardest, and you failed miserably. The lesson is: Never try.
      Weirdly i get an error (after executing on the domain controller itself )

      GetServers failed: Inappropriate I/O control operation

Re: Script to capture logged on users
by VSarkiss (Monsignor) on Aug 26, 2005 at 16:58 UTC