Discussion:
Convert LastLogonTimeStamp to DateTime in vb.net
(too old to reply)
b***@gmail.com
2005-11-04 22:09:38 UTC
Permalink
I am fetching the LastLogonTimeStamp value (Long) from AD using vb.net
DirectorySearcher code. What I can't understand is how to convert the
Long value to a datetime value, without resorting to w32tm, or
something external. I can see plenty of ways to do it in vbscript, but
what about vb.net? Preferably without the compatibility dll...

Dim entry As New DirectoryServices.DirectoryEntry("LDAP://"
& aDomains(i))
Dim output, strFilter As String, intlastlogontime As Long
output = ""
strFilter =
"(&(objectclass=computer)(operatingSystem=Windows XP Professional))"

Dim oSearcher As New
System.DirectoryServices.DirectorySearcher(entry, strFilter)
Dim oResults As SearchResultCollection
Dim oResult As SearchResult

oSearcher.SearchScope = SearchScope.Subtree
oSearcher.ReferralChasing = ReferralChasingOption.All
oSearcher.PageSize = 1000
oSearcher.PropertiesToLoad.AddRange(New String() {"name",
"createTimeStamp", "lastlogontimestamp"})

oResults = oSearcher.FindAll
If oResults.Count <> Nothing Then
If oResults.Count <> 0 Then
'iterate the collection
Dim name, ct As String, llt As Long, llt2 As
DateTime
For Each oResult In oResults
Try
name = CStr(oResult.Properties("name")(0))
Catch ex As Exception
name = ""
End Try
Try
ct =
CStr(oResult.Properties("createtimestamp")(0))
Catch ex As Exception
ct = ""
End Try
Try
llt =
CLng(oResult.Properties("lastlogontimestamp")(0))
Catch ex As Exception

End Try

'*******HERE'S HOW THEY SAY TO DO IT IN VBSCRIPT (WON'T WORK IN
.NET)*****
'intlastlogontime = llt.Highpart * (2 ^ 32) +
llt.LowPart
'intLastLogonTime = intLastLogonTime / (60 *
10000000)
'intLastLogonTime = intLastLogonTime / 1440
'***************************************************************************************************
fw.WriteLine(name & ControlChars.Tab & ct &
ControlChars.Tab & llt.ToString)
Next
End If
End If
Joe Kaplan (MVP - ADSI)
2005-11-05 02:34:54 UTC
Permalink
It is much easier in .NET. If you are using the DirectorySearcher, then the
value is already marshaled as a .NET Int64. From there, you just call
DateTime.FromFileTime and you are done.

If you get the value from the DirectoryEntry, then it will be returned as an
IADsLargeInteger and you have to reassemble it first.

Joe K.
Post by b***@gmail.com
I am fetching the LastLogonTimeStamp value (Long) from AD using vb.net
DirectorySearcher code. What I can't understand is how to convert the
Long value to a datetime value, without resorting to w32tm, or
something external. I can see plenty of ways to do it in vbscript, but
what about vb.net? Preferably without the compatibility dll...
Dim entry As New DirectoryServices.DirectoryEntry("LDAP://"
& aDomains(i))
Dim output, strFilter As String, intlastlogontime As Long
output = ""
strFilter =
"(&(objectclass=computer)(operatingSystem=Windows XP Professional))"
Dim oSearcher As New
System.DirectoryServices.DirectorySearcher(entry, strFilter)
Dim oResults As SearchResultCollection
Dim oResult As SearchResult
oSearcher.SearchScope = SearchScope.Subtree
oSearcher.ReferralChasing = ReferralChasingOption.All
oSearcher.PageSize = 1000
oSearcher.PropertiesToLoad.AddRange(New String() {"name",
"createTimeStamp", "lastlogontimestamp"})
oResults = oSearcher.FindAll
If oResults.Count <> Nothing Then
If oResults.Count <> 0 Then
'iterate the collection
Dim name, ct As String, llt As Long, llt2 As
DateTime
For Each oResult In oResults
Try
name = CStr(oResult.Properties("name")(0))
Catch ex As Exception
name = ""
End Try
Try
ct =
CStr(oResult.Properties("createtimestamp")(0))
Catch ex As Exception
ct = ""
End Try
Try
llt =
CLng(oResult.Properties("lastlogontimestamp")(0))
Catch ex As Exception
End Try
'*******HERE'S HOW THEY SAY TO DO IT IN VBSCRIPT (WON'T WORK IN
.NET)*****
'intlastlogontime = llt.Highpart * (2 ^ 32) +
llt.LowPart
'intLastLogonTime = intLastLogonTime / (60 *
10000000)
'intLastLogonTime = intLastLogonTime / 1440
'***************************************************************************************************
fw.WriteLine(name & ControlChars.Tab & ct &
ControlChars.Tab & llt.ToString)
Next
End If
End If
b***@gmail.com
2005-11-07 14:07:10 UTC
Permalink
Thanks! Works great!
b***@gmail.com
2005-11-07 14:12:43 UTC
Permalink
Thanks! Works great.
Perhaps you could answer one more question: This has been written as a
console application. How do I pass the program a parameter on the
command line? Specifically, I want to pass the output filename & path
as an argument to the program:

Module FindWS

Public Sub Main() - do I just add a paramter like this?:
Public Sub Main(byVal filename as String)

Or how? Thanks so much, either way!
Joe Kaplan (MVP - ADSI)
2005-11-07 16:13:36 UTC
Permalink
Check out the System.Environment.CommandLine or GetCommandLineArgs
property/method to read in the current command line.

There are also some samples out there in various places that show some more
advanced approaches for parsing the arguments and doing tricky things if you
want more power.

Joe K.
Post by b***@gmail.com
Thanks! Works great.
Perhaps you could answer one more question: This has been written as a
console application. How do I pass the program a parameter on the
command line? Specifically, I want to pass the output filename & path
Module FindWS
Public Sub Main(byVal filename as String)
Or how? Thanks so much, either way!
b***@gmail.com
2005-11-07 14:32:36 UTC
Permalink
Thank you so much, works great!
Loading...