I am trying to find a way of getting a list of UserPrincipal objects that have not logged in for a year. There is a Last Logon property on the object, but I can't figure out how to do a less than style of query, everything I have seen are only equal to and I can't find anything that uses the property in the search.
Am I barking up the wrong tree with this not being possible?
This is the code I have so far, the "issue" I have is that I am having to develop this blind and I am not able to run it until it goes to the customers system. At the moment it is just throwing a generic error complaining that something is null, but no reference to what object or line. Which to me is barmy as it is in a try with a catch.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim domain As PrincipalContext = New PrincipalContext(ContextType.Domain, "DomainNameHere")
Dim up As New UserPrincipal(domain)
Dim search As New PrincipalSearcher()
Dim results As PrincipalSearchResult(Of UserPrincipal)
Dim listResults As New List(Of UserPrincipal)
'Dim usr As UserPrincipal = UserPrincipal.FindByIdentity(domain, HttpContext.Current.User.Identity.Name)
Try
dt = New DataTable
dt.Columns.Add(New DataColumn("PUID", GetType(String)))
dt.Columns.Add(New DataColumn("Name", GetType(String)))
dt.Columns.Add(New DataColumn("Role", GetType(String)))
dt.Columns.Add(New DataColumn("LastLogon", GetType(String)))
dt.Columns.Add(New DataColumn("GroupCount", GetType(String)))
'dt.Rows.Add(usr.SamAccountName, usr.GivenName & " " & usr.Surname, usr.DisplayName, usr.LastLogon.ToString)
search.QueryFilter = up
results = (From principal In search.FindAll() Select TryCast(principal, UserPrincipal))
listResults = results.Where(Function(u) u.LastLogon < Today.AddYears(-1)).OrderBy(Function(u) u.LastLogon).ToList
'results = UserPrincipal.FindByLogonTime(domain, Now.AddYears(-1), MatchType.LessThanOrEquals)
'results.OrderBy(Function(u) u.LastLogon)
For Each u As UserPrincipal In listResults
If Not IsNothing(u) Then
Dim puid As String = "-"
Dim name As String = "-"
Dim role As String = "-"
Dim logon As String = "-"
Dim groupCount As String = "0"
If Not IsNothing(u.SamAccountName) Then puid = u.SamAccountName
If Not IsNothing(u.GivenName) AndAlso Not IsNothing(u.Surname) Then puid = u.GivenName & " " & u.Surname
If Not IsNothing(u.DisplayName) Then role = u.DisplayName
'If Not IsNothing(u.DisplayName) AndAlso u.DisplayName.IndexOf("(") > 5 Then role = Left(Right(u.DisplayName, u.DisplayName.Length - u.DisplayName.IndexOf("(") - 1), Right(u.DisplayName, u.DisplayName.Length - u.DisplayName.IndexOf("(") - 1).Length - 1)
If Not IsNothing(u.LastLogon) AndAlso IsDate(u.LastLogon) Then logon = CDate(u.LastLogon.ToString).ToString("dd MMM yyyy")
'Dim groups As PrincipalSearchResult(Of Principal) = u.GetGroups
'Dim listGroups As New List(Of String)
'Dim i As Integer = 0
'For Each grp As Principal In groups
' If grp.ToString.ToUpper.Contains("NAVY") Then i += 1
'Next
'groupCount = groups.Count.ToString & " (" & i.ToString & ")"
dt.Rows.Add(puid, name, role, logon, groupCount)
End If
Next
rUsers.DataSource = dt
rUsers.DataBind()
Catch ex As Exception
litError.Text = "Domain: " & domain.ToString & "<br />"
litError.Text += "User Principal: " & up.ToString & "<br /><br />"
litError.Text += "Source: " & ex.Source.ToString & "<br /><br />Message: " & ex.Message.ToString & "<br /><br />Inner Exception:<br />" & ex.InnerException.ToString & "<br /><br />Stack Trace:<br />" & ex.StackTrace
End Try
End Sub