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
Horrible mistake on my part. My problem was with the DataTable, I was overwriting the DataTable for each DC. Which is why only the last DC results were displayed. Below is what I have working although there is probably a better way to do this.
Public Function Query() As Data.DataTable
Dim context As DirectoryContext = New DirectoryContext(DirectoryContextType.Domain, "MYDOMAIN")
Dim dcc As DomainControllerCollection = DomainController.FindAll(context)
' Get the distinguishedName and lastLogon for each user
Dim dLastLogons As New Dictionary(Of String, Int64)()
Dim distinguishedName As String
Dim lastLogonThisServer As New Int64()
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("distinguishedName")
ds.PropertiesToLoad.Add("lastLogon")
ds.PageSize = 1000
ds.SearchScope = SearchScope.Subtree
ds.Sort.PropertyName = "lastLogon"
Dim src As SearchResultCollection
src = ds.FindAll()
For Each sr As SearchResult In src
' Get the distinguishedName and lastLogon for each user
distinguishedName = sr.Properties("distinguishedName")(0).ToString()
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 (dLastLogons.ContainsKey(distinguishedName)) Then
If (dLastLogons(distinguishedName) < lastLogonThisServer) Then
dLastLogons(distinguishedName) = lastLogonThisServer
End If
Else
dLastLogons.Add(distinguishedName, lastLogonThisServer)
End If
Next
pnlSearchResults.Visible = True
ltlSearchResultsCount.Text = src.Count()
End Using
End Using
Next
Dim dt As New Data.DataTable
Dim dr As Data.DataRow
Dim strArray() As String = Nothing
Dim strKey As String = Nothing
dt.Columns.Add("DisplayName", GetType(String))
dt.Columns.Add("LastLogon", GetType(String))
For Each kvp1 As KeyValuePair(Of String, Int64) In dLastLogons
dr = dt.NewRow
dr(0) = kvp1.Key
Dim readableLastLogon As String = DateTime.FromFileTime(kvp1.Value).ToString()
dr(1) = readableLastLogon
dt.Rows.Add(dr)
Next
Return dt
End Function
Marked as answer by w_nairb on Nov 10, 2008 06:08 PM
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.w_nairb
Member
114 Points
37 Posts
Re: Generating LastLogon list but results returned only from the last DC?
Nov 10, 2008 06:07 PM|LINK
Horrible mistake on my part. My problem was with the DataTable, I was overwriting the DataTable for each DC. Which is why only the last DC results were displayed. Below is what I have working although there is probably a better way to do this.
Public Function Query() As Data.DataTable Dim context As DirectoryContext = New DirectoryContext(DirectoryContextType.Domain, "MYDOMAIN") Dim dcc As DomainControllerCollection = DomainController.FindAll(context) ' Get the distinguishedName and lastLogon for each user Dim dLastLogons As New Dictionary(Of String, Int64)() Dim distinguishedName As String Dim lastLogonThisServer As New Int64() 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("distinguishedName") ds.PropertiesToLoad.Add("lastLogon") ds.PageSize = 1000 ds.SearchScope = SearchScope.Subtree ds.Sort.PropertyName = "lastLogon" Dim src As SearchResultCollection src = ds.FindAll() For Each sr As SearchResult In src ' Get the distinguishedName and lastLogon for each user distinguishedName = sr.Properties("distinguishedName")(0).ToString() 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 (dLastLogons.ContainsKey(distinguishedName)) Then If (dLastLogons(distinguishedName) < lastLogonThisServer) Then dLastLogons(distinguishedName) = lastLogonThisServer End If Else dLastLogons.Add(distinguishedName, lastLogonThisServer) End If Next pnlSearchResults.Visible = True ltlSearchResultsCount.Text = src.Count() End Using End Using Next Dim dt As New Data.DataTable Dim dr As Data.DataRow Dim strArray() As String = Nothing Dim strKey As String = Nothing dt.Columns.Add("DisplayName", GetType(String)) dt.Columns.Add("LastLogon", GetType(String)) For Each kvp1 As KeyValuePair(Of String, Int64) In dLastLogons dr = dt.NewRow dr(0) = kvp1.Key Dim readableLastLogon As String = DateTime.FromFileTime(kvp1.Value).ToString() dr(1) = readableLastLogon dt.Rows.Add(dr) Next Return dt End Function