Hey Folks,
Odd problem I've been dealing with at work and I just cannot figure it out. Here's the details and some pseudo code.
Problem: Setting expiration date with Set-ADUser -AccountExpirationDate or Set-ADAccountExpiration with a DateTime object produced by Get-Date uses local time, making expiration time differ on the dc in a different time zone where it's being set. The time should be 00:00.
Details:
Server 2022 running in aws with a UTC time zone. Being used by gitlab pipeline for script execution. DC is located in the EST time zone.
Sample code
# All of this happens on the runner server
$date = "2023-10-15"
$date = Get-Date $date -Hour 0 -Minute 0 -Second 0
$timeZoneConvertedDate = [System.TimeZoneInfo]::ConvertTimeBySystemTimeZoneId($date,'Eastern Standard Time')
# This compensates for time difference
if($timeZoneConvertedDate.Hour -ne 0){
timeZoneConvertedDated = $timeZoneConvertedDated.AddHour((24 - $timeZoneConvertedDate.Hour))
}
$fakeUser | Set-ADAccountExpiration $timeZoneConvertedDate -Server EasternStandardDC.contoso.com
This seems to always set the user with an offset depending on the server time, despite the DateTime object in the pipeline showing 00:00 on the targeted Time Zone.
I noticed the documentation for both Set-ADUser and Set-ADAccountExpiration mention this, but never elaborate!
Time is assumed to be local time unless otherwise specified.
https://learn.microsoft.com/en-us/powershell/module/activedirectory/set-adaccountexpiration?view=windowsserver2022-ps#-datetime
Any ideas?