0

I'm attempting to use Active Directory extensionAttributes to keep track of certain dates (like start date, termination date, etc) so that I can trigger certain actions when that date occurs.

I'm having an issue with the different variations that a date can be entered in (M/D/YY, MM/DD/YY, MM/DD/YYYY, etc). For example, I can use Get-Date to output to a format of M/D/YYYY, but I run into issues when someone enters MM/DD/YY.

Is there a way to make this work so that it can accept other variations (as long as it's month/date/year)?

Here are a couple of lines from the script in question. This runs once a day, and checks for new users starting the following day.

$StartingOn = (Get-Date).AddDays(1).ToShortDateString()

$NewUserCheck = Get-QADUser -DontUseDefaultIncludedProperties -IncludedProperties extensionAttribute11 | where { $_.extensionAttribute11 -eq $StartingOn }

Notice how it only returns as long as the date equals the Get-Date output. It was the only way I was able to get this to work properly. Even at that, if someone typed in 07/20/15, the output would return nothing.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328

1 Answers1

3

Don't try to compare date strings. Use DateTime comparison which won't care about formatting details e.g.:

$StartingOn = (Get-Date).AddDays(1)
$NewUserCheck = Get-QADUser -DontUseDefaultIncludedProperties -IncludedProperties extensionAttribute11 | 
    Where { [DateTime]($_.extensionAttribute11) -eq $StartingOn}
Keith Hill
  • 194,368
  • 42
  • 353
  • 369
  • Interesting. Thanks for the help. However, I received multiple "Cannot convert null to type "System.DateTime". Not sure what this means exactly. – user2184777 Jul 23 '15 at 19:03
  • n/m. I understand the problem now. Looking for a way around it. Is there something I can add in this command to accept null as a value? – user2184777 Jul 23 '15 at 19:09
  • Where is the null? $_ or $_.extensionAttributes11? – Keith Hill Jul 23 '15 at 20:23
  • The repeated errors are coming from the amount of empty extensionAttribute11 from the rest of the user base. As a workaround, I populated everyone else's extensionAttribute11 with an arbitrary date. your edited script worked great afterwards! – user2184777 Jul 24 '15 at 02:55