Okay, this was such a pain in the buttocks that I decided to post the
"final" code. (I'll tweak it a bit later, but this is functional)  My
apologies to someone, I found the pack(),unpack() part on the Internet,
and I'm not sure who the original author is.

####################################################

use strict;
use warnings;
no warnings qw(uninitialized);
use Win32::OLE qw(in);
use Win32::OLE::Variant;
use Tim::Date_Time;
use Tim::GetClients;

my @dc = qw(dc1 dc2 dc3 dc4);
print "\n\nFinding all available DCs...\n\n";
for(0..$#dc){
        unless(GetClients::Ping($dc[$_])){
                delete $dc[$_];
        }
}
my %users;

foreach my $dc(@dc){
        print "Checking $dc";
        my $ADUser = Win32::OLE->GetObject("LDAP://$dc/OU=Groups and
Users,OU=HQ,DC=domain,DC=com") || die;
        foreach(in($ADUser)){
                unless($_->{objectCategory} =~ /Group/i){
                        my $lastlogon = $_->{lastLogon};
                        my $name = $_->{name};
                        my $lastlogontime = 1;
                        if($lastlogon){
                          my $Hval = $lastlogon->{HighPart};
                          my $Lval = $lastlogon->{LowPart};
                          my $Factor = 10000000;   # convert to seconds
                          my $uPval = pack("II",$Lval,$Hval);
                          my($bVp, $aVp) = unpack("LL", $uPval);
                          $uPval = ($aVp * 2**32 + $bVp)/$Factor;
                          $lastlogontime = $uPval - (134774*86400);
#convert to Perl time
                        }
                        print ".";
                        $name =~ s/^CN=//;
                        push @{$users{$name}},$lastlogontime;
                }
        }
        print "\n\n"
}

open(OUTFILE,">lastlogon.csv") || die;
print "Finding last logon...\n";
foreach(sort keys %users){
        my $name = $_;
        my $lastlogon;
        foreach my $logon(@{$users{$_}}){
                if($logon > $lastlogon){
                        $lastlogon = $logon;
                }
        }
        $lastlogon = (Date_Time::SimpleDT($lastlogon))[0];
        print OUTFILE "$name,$lastlogon\n";
}
close OUTFILE;

######################################################

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to