This is not really my code...  it was stolen with pride from the list
archives....  Please google this type of stuff first... and in general,
this list likes plain text posts, so you reach a wider audience if you
post in plain text....  This is friendly advice. 

#1
Original post for this solution found here:
http://aspn.activestate.com/ASPN/Mail/Message/pdk/2166533

#the lines of code that was given above, doesn't pertain to this
GetObject call and isn't needed.
my $adsuser = Win32::OLE->GetObject("LDAP://$userDN") || die ("Can't
find user: ".Win32::OLE->LastError()."\n");
#the line above was modified, as the syntax looked like an ad search
instead of a straight LDAP get object..  If it works the way you had it,
then I don't care..  But it looked off.

print "CN: $adsuser->{cn}\n";
print "Username: $adsuser->{Samaccountname}\n";
 
#<from archive post>
    # Calculate password age in days
    my $PWage;
    my $LastPW  = $adsuser-> {pwdLastSet};
    my $fRef = ref ($LastPW);
    my ($Hval, $Lval);
    if ($fRef eq 'Win32::OLE' )
    {
       $Hval = $LastPW-> HighPart;
       $Lval = $LastPW-> 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;
       if ($uPval != 0)
       {
          $uPval -= 134774*86400;  #Adjust for perl time!
           my $EpochSeconds = time;
          $PWage = ($EpochSeconds - int($uPval))/(60*60*24) ;
          $PWage =~ s/\..*$//;
       }
    } 
#</from archive post>
    
    #<my contribution to this question>
    print "PWD Last set: " . localtime(time - $PWage*60*60*24) ."\n";
    #output something like this...  PWD Last set: Sun Feb 20 23:25:51
2010
    #</my contribution to this question>
 
#2   If you are looking to use different authentication credentials you
can look at using OpenDSObject instead of GetObject.  Google
OpenDSObject for examples, because it allows you to insert credentials,
but also be wary of it's use as you can/will embed plain text passwords
in those types of scripts which is a security risk at it's core.
 
HTH
 
Steven

________________________________

        From: perl-win32-admin-boun...@listserv.activestate.com
[mailto:perl-win32-admin-boun...@listserv.activestate.com] On Behalf Of
A F
        Sent: Thursday, July 15, 2010 5:56 PM
        To: perl-win32-admin@listserv.ActiveState.com
        Subject: Active Directory question
        
        
        Hi All,
         
        Can someone help me with these 2 question that I have?
        1) How can I print the date of the Last password set? It is
telling me that it is a hash.
        2) Can I specify a specific DomainName\UserID to the Win32::OLE
object and search for that user info instead just getting the current
user logged on info that run the program?
         
        Thanks
        Alain
         
        use Win32::OLE;
        use strict;
         
        my $sysinfo = Win32::OLE->new('ADSystemInfo') || die ("Can't get
sysinfo: " .Win32::OLE->LastError()."\n");
        my $userDN=$sysinfo->{UserName};
         
        my $adsuser = Win32::OLE->GetObject("LDAP://$userDN
<ldap://$userDN/> ") || die ("Can't find user:
".Win32::OLE->LastError()."\n");
        print "CN: $adsuser->{cn}\n";
        print "Username: $adsuser->{Samaccountname}\n";
        print "Last set: $adsuser->{pwdLastSet}\n";
         
         
        This is my output:
        CN: Smith, James
        Username: jsmith
        Last set: Win32::OLE=HASH(0x19a3b04)


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

Reply via email to