Comparing dates

What am i doing wrong?
I need to get all users which did not sign in interactively on noninteractively in the last 425 days. For this i need to look at two attributes:

  1. LastSignInDateTime
  2. LastNonInteractiveSignInDateTime

I also want to filter out all objects where the invite has been sent this year.
My arrays conains folliwing objects

PS C:\VSCode\PS> $UnfilteredStaleAccounts | Get-Member -MemberType Noteproperty | select Name

Name
----
CreatedDateTime
DisplayName
ExternalUserState
Id
LastNonInteractiveSignInDateTime
LastSignInDateTime

Is use following filter

$StaleGuests = $UnfilteredStaleAccounts | Where-Object {($_.LastSignInDateTime -lt (Get-Date).AddDays(-425)) -or ($_.LastNonInteractiveSignInDateTime -lt (Get-Date).AddDays(-425)) -and ($_.CreatedDateTime -notlike (Get-Date).Year)}

When i run the filter i get all objects where LastNonInteractiveSignInDateTime has a date, even dates newer then 425 days.

LastNonInteractiveSignInDateTime LastSignInDateTime
-------------------------------- ------------------
25/03/2024 09:41:59
25/03/2024 09:40:20
25/03/2024 09:38:01
25/03/2024 09:33:12
25/03/2024 09:33:06
25/03/2024 09:32:20
25/03/2024 09:31:46
25/03/2024 09:29:50
25/03/2024 09:29:37
25/03/2024 09:19:46
25/03/2024 09:17:54
25/03/2024 09:17:17
25/03/2024 09:13:56
25/03/2024 09:11:11

Hello,

You use “-or” and “-and” without explicite priority, I means, without parenthesis that put explicite priority of “-and” and “-or”.
I think you may look at that !

1 Like

Don’t think this part will ever be true
(Get-Date).Year is an int
CreatedDateTime is either a string or datetime object??? If it’s a datetime or converted to one then you could say

($_.CreatedDateTime.Year -notlike (Get-Date).Year)

Simplify your approach. Instead of stringing together a more complicated where-object scriptblock, filter out the accounts that were created this year. It’s not only going to make it easier for you to write, but it’ll make it easier for someone else to read later down the road. You also should be careful using the notlike on the date like that. No reason to do that if you have a datetime object to begin with, you can simply check the Year property and see if it matches the current year, if it doesn’t you can exclude it. For example:

$Users = Get-MGuser -all -Property ID, Mail, UserPrincipalName, Signinactivity,CreatedDateTime -Top 100
$CurrentYear = (Get-Date).Year
$UserList = $Users | Where-Object { $_.CreatedDateTime.Year -ne $CurrentYear}

This gets you at a better starting place.

1 Like

I filtered the objects first on date as adviced:

$UnfilteredStaleAccounts = $UnfilteredStaleAccounts | Where-Object {$_.CreatedDateTime.year -ne $CurrentYear}

Then filtered the remaining results on the two conditions

$StaleGuests = $UnfilteredStaleAccounts | Where-Object {($_.LastSignInDateTime -lt (Get-Date).AddDays(-425)) -or ($_.LastNonInteractiveSignInDateTime -lt (Get-Date).AddDays(-425))}

But the array staleguest still contains objects which are newer then 425 days ago.
I have checked the noteproperties, and both LastSignInDateTime and LastNonInteractiveSignInDateTime are listed as System.DateTime

PS C:\VSCode\PS> $staleguests | Get-Member

   TypeName: Selected.Microsoft.Graph.Beta.PowerShell.Models.MicrosoftGraphUser

Name                             MemberType   Definition
----                             ----------   ----------
Equals                           Method       bool Equals(System.Object obj)
GetHashCode                      Method       int GetHashCode()
GetType                          Method       type GetType()
ToString                         Method       string ToString()
CreatedDateTime                  NoteProperty datetime CreatedDateTime=04/08/2019 08:41:45
DisplayName                      NoteProperty string DisplayName=Burggraeve Marianne
ExternalUserState                NoteProperty string ExternalUserState=Accepted
Id                               NoteProperty string Id=63e8bb22-7fc1-42ba-a25f-9e89be654e57
LastNonInteractiveSignInDateTime NoteProperty System.DateTime LastNonInteractiveSignInDateTime=25/03/2024 21:52:17
LastSignInDateTime               NoteProperty object LastSignInDateTime=null

Found my foolish mistake, it shouldn’t be ‘-or’ but ‘-and’.

Yea alastor mentioned that my comment was an additional tip to help. I should have clarified that though apologies.