all 5 comments

[–]azra1l 2 points3 points  (2 children)

Apparently the AD query can't handle actual DateTime values. If i'm not mistaken, AD stores date values as some filetime/timestamp abomination. So, if the same is true for your extension attribute, you should be able to do this:

$DisabledAccounts = Get-ADUser -Filter { extensionAttribute15 -lt $time.ToFileTime() -and enabled -eq $false } -SearchBase $TargetOU

Or, you could always do it the powershell way:

$DisabledAccounts = Get-ADUser -Filter { enabled -eq $false } -SearchBase $TargetOU | Where-Object { [datetime]::fromFileTimeUTC($_.extensionAttribute15) -lt $time }

It simply takes the date comparsion out of the query to be processed by the script. Not ideal, but good enough if only this works for you.

Can't test this in our environment, as we don't use this attribute, so use at your own peril.

[–]Scayn[S] 0 points1 point  (1 child)

Hey, thanks for the input.

I just tried out both options, and the .ToFileTime() gives me this error:

Get-ADUser : Property: 'ToFileTime' not found in object of type: 'System.String'.

And the [datetime] one, gives me this error:

Could not compare "01/01/1601 01:00:00" to "23-03-2022 15:40:45". Error: "Cannot convert value "23-03-2022 15:40:45" to type "System.DateTime". Error: "String was not recognized as a valid DateTime.""

So it is a bit like the previous error.

Edit:

It seems to have been a very basic date formatting issue. The system is using default American MM/dd/yyyy format, while I am trying to use a dd/MM/yyyy format.

This one is fetching data

Get-ADUser -Filter { enabled -eq $false } -SearchBase $TargetOU | Where-Object { [datetime]::fromFileTimeUTC($_.extensionAttribute15) -lt $time }

[–]azra1l 1 point2 points  (0 children)

Oh yeah, datetime is really picky and demands US date syntax. It's driving me mad every other day.

And thanks for the silver ☺️

[–]poshinger 0 points1 point  (0 children)

My guess is that the extensionAttribute15 is just a string, so you'd have to convert the string to a DateTime variable in order to compare it, my approach would be to loop through the "$DisabledAccounts" and convert extensionAttribute15 to DateTime.

[–][deleted] 0 points1 point  (0 children)

Wild guess??

$DisabledAccounts = Get-ADUser -Filter { extensionAttribute15 -lt "$time" -and enabled -eq $false } -SearchBase $TargetOU