I would like to generate a list of users lastLogon. The code works however when it polls through the DC's it returns a list of users and their lastlogon from the last DC polled. Am I missing something? I
appreciate any suggestions.
Public Function Query(ByVal strLastLogonTime As String) As Data.DataTable
Dim context As DirectoryContext = New DirectoryContext(DirectoryContextType.Domain, "MYDOMAIN")
Dim dcc As DomainControllerCollection = DomainController.FindAll(context)
For Each dc As DomainController In dcc
Dim s As String = Nothing
s = dc.Name.ToString()
'Get all the users for that domain
Dim de As New DirectoryEntry("LDAP://" & s & "/" & AppSettings("AdPath").ToString())
Dim AdFilter As String = "(&(objectClass=user)(objectCategory=person)(!userAccountControl:1.2.840.113556.1.4.803:=2))"
Using de
Dim ds As New DirectorySearcher(de, AdFilter)
Using ds
ds.PropertiesToLoad.Add("cn")
ds.PropertiesToLoad.Add("distinguishedName")
ds.PropertiesToLoad.Add("lastLogon")
ds.PageSize = 1000
ds.SearchScope = SearchScope.Subtree
ds.Sort.PropertyName = "lastLogon"
Dim src As SearchResultCollection
src = ds.FindAll()
Dim dt As New Data.DataTable
Dim dr As Data.DataRow
dt.Columns.Add("distinguishedName", GetType(String))
dt.Columns.Add("lastLogon", GetType(String))
dt.Columns.Add("dc", GetType(String))
For Each sr As SearchResult In src
' Get the distinguishedName and lastLogon for each user
Dim lastLogons As New Dictionary(Of String, Int64)()
Dim distinguishedName As String = sr.Properties("distinguishedName")(0).ToString()
Dim lastLogonThisServer As New Int64()
dr = dt.NewRow
If (sr.Properties.Contains("cn")) Then
dr(0) = sr.Properties("cn")(0)
End If
If (sr.Properties.Contains("lastLogon")) Then
lastLogonThisServer = CType(sr.Properties("lastLogon")(0), Long)
End If
' Save the most recent logon for each user in a Dictionary object
If (lastLogons.ContainsKey(distinguishedName)) Then
If (lastLogons(distinguishedName) < lastLogonThisServer) Then
lastLogons(distinguishedName) = lastLogonThisServer
dr(2) = s
End If
Else
lastLogons.Add(distinguishedName, lastLogonThisServer)
dr(2) = s
End If
Dim readableLastLogon As String = DateTime.FromFileTime(lastLogonThisServer).ToString()
dr(1) = readableLastLogon
dt.Rows.Add(dr)
Next
ltlNumberOfUsers.Text = src.Count()
Return dt
End Using
End Using
Next
End Function
w_nairb
Member
114 Points
37 Posts
Generating LastLogon list but results returned only from the last DC?
Nov 06, 2008 07:40 PM|LINK
I would like to generate a list of users lastLogon. The code works however when it polls through the DC's it returns a list of users and their lastlogon from the last DC polled. Am I missing something? I appreciate any suggestions.
Public Function Query(ByVal strLastLogonTime As String) As Data.DataTable Dim context As DirectoryContext = New DirectoryContext(DirectoryContextType.Domain, "MYDOMAIN") Dim dcc As DomainControllerCollection = DomainController.FindAll(context) For Each dc As DomainController In dcc Dim s As String = Nothing s = dc.Name.ToString() 'Get all the users for that domain Dim de As New DirectoryEntry("LDAP://" & s & "/" & AppSettings("AdPath").ToString()) Dim AdFilter As String = "(&(objectClass=user)(objectCategory=person)(!userAccountControl:1.2.840.113556.1.4.803:=2))" Using de Dim ds As New DirectorySearcher(de, AdFilter) Using ds ds.PropertiesToLoad.Add("cn") ds.PropertiesToLoad.Add("distinguishedName") ds.PropertiesToLoad.Add("lastLogon") ds.PageSize = 1000 ds.SearchScope = SearchScope.Subtree ds.Sort.PropertyName = "lastLogon" Dim src As SearchResultCollection src = ds.FindAll() Dim dt As New Data.DataTable Dim dr As Data.DataRow dt.Columns.Add("distinguishedName", GetType(String)) dt.Columns.Add("lastLogon", GetType(String)) dt.Columns.Add("dc", GetType(String)) For Each sr As SearchResult In src ' Get the distinguishedName and lastLogon for each user Dim lastLogons As New Dictionary(Of String, Int64)() Dim distinguishedName As String = sr.Properties("distinguishedName")(0).ToString() Dim lastLogonThisServer As New Int64() dr = dt.NewRow If (sr.Properties.Contains("cn")) Then dr(0) = sr.Properties("cn")(0) End If If (sr.Properties.Contains("lastLogon")) Then lastLogonThisServer = CType(sr.Properties("lastLogon")(0), Long) End If ' Save the most recent logon for each user in a Dictionary object If (lastLogons.ContainsKey(distinguishedName)) Then If (lastLogons(distinguishedName) < lastLogonThisServer) Then lastLogons(distinguishedName) = lastLogonThisServer dr(2) = s End If Else lastLogons.Add(distinguishedName, lastLogonThisServer) dr(2) = s End If Dim readableLastLogon As String = DateTime.FromFileTime(lastLogonThisServer).ToString() dr(1) = readableLastLogon dt.Rows.Add(dr) Next ltlNumberOfUsers.Text = src.Count() Return dt End Using End Using Next End FunctionReferenced http://www.codeproject.com/KB/security/LastLogonAcrossAllWindows.aspx and http://forums.asp.net/p/1274419/2418941.aspx.