Fellow Monks,

I had a need today to find out when a person last logged into the network. It's a windows network, and the DC's are windows 2003 servers. So it has the one attribute needed to assist with this task, lastLogonTimestamp.

The issue with this value is though, it's a 64bit in of the number of 100 nanoseconds since 1/1/1601. Working out the date left me bit lost. Mostly because I was looking for the "right" way to do it, and I'm not saying that I have neccessarily hit on it the right way, but IF nothing else, this is at least a perl code sample for others to work on.

Of course this is a function where it's actually used, and the time stamp provided is an example only.

I hope I have it right, feel free to correct etc.

#!perl # it's run on windows that's why not a full path # Code Snippet to show how to get the date of the last login time # of an AD user on windows 2003 stored in the lastLogonTimestamp attri +bute. # # For more inforamtion about the value and windows use of Epoch, # basically put, the number of 100-nanosecond intervals that passed be +tween January 1, 1601 and the time the user last logged on # # Reference to the maths: http://www.microsoft.com/technet/scriptcente +r/topics/win2003/lastlogon.mspx # The document also covers the issue replication of this attribute, be + warned, the attribute # lastLogonTimestamp is replicated only once every 14 days. # # How to get the attribute out of AD is up to you. I was using Net::L +DAP at the time. # # Hopefully this will be found and help out anyone else stuck with thi +s problem. # use strict; # we need to handle the large int. It's a 64bit int use Math::BigInt; # Use Date::Calc to work out the days and date since 1/1/1600 use Date::Calc qw/Add_Delta_Days/; my $lastLoginTime = '127912653523035916'; my $loginTime = Math::BigInt->new( $lastLoginTime ); # Based on the document above and the words taken directly from: # there are 1,000,000,000 nanoseconds in a second; # therefore, there are 10,000,000 100-nanosecond intervals in a single + second (10,000,000 x 100 = 1,000,000,000). $loginTime = $loginTime / (60 * 10000000 ); # 60 seconds in a minute! # And because there are 1,440 minutes in every 24-hour day, this line +of code tells us how many days have elapsed: $loginTime = $loginTime / 1440; # and now, to get the date of the last login: my ($year,$month,$day) = Add_Delta_Days( 1601, 1, 1, $loginTime ); print "$year-$month-$day\n"; __END__

In reply to Calculating lastLoginTimeStamp from Active Directory by waxhead

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.