datepowershellactive-directoryquest

Working with dates in extensionAttributes and Get-Date


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.


Solution

  • 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}