Home All Groups Group Topic Archive Search About
Author
17 Jul 2006 8:30 PM
Kenneth H. Young
I am trying to write a VB app in VS.net to find the lastLogonTimestamp and
have found some example but the answer returned is always the same
'12/31/1600 7:00:00 PM' for any user account.

Thanks!

Below is a snippet of the code.

Dim Data1 As String
Dim Data3 As ActiveDs.LargeIntergerClass
Dim Data4 as Date
Din lngHigh, lngLow, timeAdj As Int64

        Dim x As Integer = 0

        ' Bind to the Default Active Directory with the RootDSE object.
        rootDSE = GetObject("LDAP://RootDSE")
        sObjectDN = "LDAP://" & rootDSE.Get("defaultNamingContext")

        Me.txtStatus.Text = "Connecting to: " & sObjectDN
        Dim EntryPath As String = ("" & sObjectDN & "")
        Entry = New DirectoryEntry(EntryPath)
        Entry.RefreshCache()
        Dim Searcher As New DirectorySearcher(Entry)
        Searcher.PageSize = 5000
        Dim strContact As String = "(&(objectClass=user)(mail=sue*))"
        Searcher.Filter = strContact
        Searcher.PropertiesToLoad.Add("cn")
        Searcher.PropertiesToLoad.Add("lastLogon")
        Searcher.PropertiesToLoad.Add("givenName")
        Searcher.PropertiesToLoad.Add("sn")
        Dim Results As System.DirectoryServices.SearchResult
        On Error Resume Next
For Each Results In Searcher.FindAll()
            Data1 = Nothing
            Data3 = Nothing
            If Not Results.GetDirectoryEntry().Properties("cn").Value =
Nothing Then
            Data1 =
Results.GetDirectoryEntry().Properties("cn").Value.ToString
            Else : GoTo Skip
            End If
            Data3 =
Results.GetDirectoryEntry().Properties("lastLogonTimestamp").Value
            lngHigh = Data3.HighPart
            lngLow = Data3.LowPart
            timeAdj = lngHigh * (2 << 32) + lngLow
            timeAdj = timeAdj / (60 * 10000000)
            timeAdj = timeAdj / 1440
            Data4 = DateTime.FromFileTime(timeAdj).ToShortDateString & " " &
DateTime.FromFileTime(timeAdj).ToShortTimeString
            Data4 = Data4 + #1/1/1601#
            Me.txtResults.Text = Me.txtResults.Text & Data1 & ", " & Data4 &
vbCrLf

Skip:

        Next

Author
18 Jul 2006 12:40 PM
Dirk_Drexlin_
Kenneth H. Young schrieb:
> I am trying to write a VB app in VS.net to find the lastLogonTimestamp and
> have found some example but the answer returned is always the same
> '12/31/1600 7:00:00 PM' for any user account.

THis means, that the value of lastLogonTimestamp is empty; probably
because the user never loged on.

Greetings

Dirk
Author
18 Jul 2006 1:17 PM
Kenneth H. Young
In later testing that's what it seemed like a Null or Nothing returm.
But I am testing in a single Active Directory server environment against
known active accounts.


Show quoteHide quote
"Dirk_Drexlin_" <nospamdrexlin@nospamhgs-calw.de> wrote in message
news:4i432eF1nvocU1@individual.net...
> Kenneth H. Young schrieb:
>> I am trying to write a VB app in VS.net to find the lastLogonTimestamp
>> and
>> have found some example but the answer returned is always the same
>> '12/31/1600 7:00:00 PM' for any user account.
>
> THis means, that the value of lastLogonTimestamp is empty; probably
> because the user never loged on.
>
> Greetings
>
> Dirk
Author
18 Jul 2006 6:14 PM
Kenneth H. Young
OK, I have tested over and over again. I have set the code up to pull
back all users and then tweaked it to pull from two different servers and
the results for every user is always the same for lastLogonTimeStamp
'12/31/1600 7:00:00 PM'.  Any help with this will be greatly appreciated.
Below is all my code, please excuse the mess.

Imports System.DirectoryServices
Imports System.Windows.Forms
Imports ActiveDs
Imports ADODB

Public Class Form1
    Dim Entry As DirectoryEntry
    Dim Searcher As DirectorySearcher
    Dim rootDSE As IADs
    Dim sObjectDN As String
    Dim timeAdj As Int64
    Dim lngHigh As Int64
    Dim lngLow As Int64

    Private Sub btLastLogon_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btLastLogon.Click
        Dim tDate As DateTime
        Dim valLL As Date
        tDate = Date.Now().AddDays(-30).ToShortDateString
        Me.TextBox1.Text = tDate
        Dim Data1, Data2 As String
        Dim Data3 As ActiveDs.LargeInteger
        Dim Data4, Data5 As Date
        Dim x As Integer = 0
        Dim llastLogonAdjust As Long = 127877417297554938L

        ' Bind to the Default Active Directory with the RootDSE object.
        rootDSE = GetObject("LDAP://RootDSE")
        sObjectDN = "LDAP://" & rootDSE.Get("defaultNamingContext")

        Me.txtStatus.Text = "Connecting to: " & sObjectDN
        Dim EntryPath As String = ("" & sObjectDN & "")
        Entry = New DirectoryEntry(EntryPath)
        Entry.UsePropertyCache = False
        Dim Searcher As New DirectorySearcher(Entry)
        Searcher.PageSize = 5000
        Dim strContact As String = "(&(objectClass=user)(cn=*))"
        Searcher.Filter = strContact
        Searcher.PropertiesToLoad.Add("cn")
        Searcher.PropertiesToLoad.Add("lastLogon")
        Searcher.PropertiesToLoad.Add("givenName")
        Searcher.PropertiesToLoad.Add("sn")
        Searcher.PropertiesToLoad.Add("lastLogonTimestamp")
        Dim Results As System.DirectoryServices.SearchResult
        On Error Resume Next
        For Each Results In Searcher.FindAll()
            If Not Results.GetDirectoryEntry().Properties("cn").Value =
Nothing Then
                Data1 =
Results.GetDirectoryEntry().Properties("cn").Value.ToString
            Else : GoTo Skip
            End If
            Data3 =
Results.GetDirectoryEntry().Properties("lastLogonTimestamp").Value
            lngHigh = Data3.HighPart
            lngLow = Data3.LowPart
            timeAdj = lngHigh * (2 << 32) + lngLow
            timeAdj = timeAdj / (60 * 10000000)
            timeAdj = timeAdj / 1440
            Data4 = DateTime.FromFileTime(timeAdj).ToShortDateString & " " &
DateTime.FromFileTime(timeAdj).ToShortTimeString
            Data4 = Data4 + #1/1/1601#
            Me.txtResults.Text = Me.txtResults.Text & Data1 & ", " & Data4 &
vbCrLf
            x += 1
Skip:

        Next
        Entry.Close()
        'Me.txtStatus.Text = "Found " & x & " entries."
    End Sub
End Class
Author
19 Jul 2006 6:57 AM
Dirk_Drexlin_
Are you sure, the problem is your program and not the Active Directory?

What value do you see when you check the properties of a user in the AD?

Greetings

Dirk
Author
19 Jul 2006 12:24 PM
Kenneth H. Young
I have checked a large number of the users on the server using LDP as well
as Active Directory Users & Computers 'Additional Account Info' tab and they
are are populated with a date.



Show quoteHide quote
"Dirk_Drexlin_" <nospamdrexlin@nospamhgs-calw.de> wrote in message
news:4i63b2F27fc0U1@individual.net...
> Are you sure, the problem is your program and not the Active Directory?
>
> What value do you see when you check the properties of a user in the AD?
>
> Greetings
>
> Dirk
Author
20 Jul 2006 4:16 PM
Travis Sharpe
Anytime I retrieve the lastlogon value from AD, I use the following
calculation, it works fine for me.

Dim UserDE as new
DirectoryEntry("LDAP://cn=sharpe\,travis,ou=users,dc=cahs,dc=colostate,dc=edu")

Dim highpart,lowpart,lastlogon as long
highpart = UserDE.Properties("LastLogonTimeStamp")(0).HighPart
Lowpart = UserDE.Properties("LastLogonTimeStamp")(0).Lowpart
lastLogon = (HighPart * 2^32) - Lowpart

msgbox DateTime.FromFileTime(lastLogon)


Kenneth H. Young wrote:
Show quoteHide quote
> I have checked a large number of the users on the server using LDP as well
> as Active Directory Users & Computers 'Additional Account Info' tab and they
> are are populated with a date.
>
>
>
> "Dirk_Drexlin_" <nospamdrexlin@nospamhgs-calw.de> wrote in message
> news:4i63b2F27fc0U1@individual.net...
>> Are you sure, the problem is your program and not the Active Directory?
>>
>> What value do you see when you check the properties of a user in the AD?
>>
>> Greetings
>>
>> Dirk
>
>
Author
21 Jul 2006 11:15 AM
Kenneth H. Young
THANK YOU!

That works and is the simplest solution I have seen to date.


Show quoteHide quote
"Travis Sharpe" <u***@anon.com> wrote in message
news:eWaKdfBrGHA.1648@TK2MSFTNGP02.phx.gbl...
> Anytime I retrieve the lastlogon value from AD, I use the following
> calculation, it works fine for me.
>
> Dim UserDE as new
> DirectoryEntry("LDAP://cn=sharpe\,travis,ou=users,dc=cahs,dc=colostate,dc=edu")
>
> Dim highpart,lowpart,lastlogon as long
> highpart = UserDE.Properties("LastLogonTimeStamp")(0).HighPart
> Lowpart = UserDE.Properties("LastLogonTimeStamp")(0).Lowpart
> lastLogon = (HighPart * 2^32) - Lowpart
>
> msgbox DateTime.FromFileTime(lastLogon)
>
>
> Kenneth H. Young wrote:
>> I have checked a large number of the users on the server using LDP as
>> well as Active Directory Users & Computers 'Additional Account Info' tab
>> and they are are populated with a date.
>>
>>
>>
>> "Dirk_Drexlin_" <nospamdrexlin@nospamhgs-calw.de> wrote in message
>> news:4i63b2F27fc0U1@individual.net...
>>> Are you sure, the problem is your program and not the Active Directory?
>>>
>>> What value do you see when you check the properties of a user in the AD?
>>>
>>> Greetings
>>>
>>> Dirk
>>