I am writing a script that shows the amount of days left until we should change a password. The problem is that right now, we are about 11 days until the password needs to change. I'm struggling with the logic to script this out. The beginning of the 85 day count down was May 16, 2021. The next 85 days starts August 9, 2021. I would like the days to reset after 85. I'm honestly not even sure how to start this. Maybe a for-loop that returns 85 - $currentDate and then resets after 85 but I'm not sure how to get it to start today in the middle of the 85 days.
In your script, you can create a starting date of May 16, 2021
and increment that date by 85 days as needed:
# Original Start Date at 12:00 AM
$85Date = [datetime]'May 16, 2021'
# Today's date
$Current = Get-Date
# checks if today is past the 85 day date
# if you didn't change the password with 0 days remaining, this logic fails
while ($Current.Date -gt $85Date) {
# increments $85Date by 85 days
$85Date = $85Date.AddDays(85)
}
# check when there are 0 days remaining
if ($Current.Date -eq $85Date) {
# Send alert here for 0 days remaining
}
# output how many days remain
'{0} days remaining until password expiration' -f ($85Date - $current.Date).Days
The code as it is won't know if the password was updated unless you add that logic. It will just assume you waited until day 85 to reset it each time. To add more intelligence, you would need to then do one of the following:
There are going to be many alternatives to making this work. As of now, we do not know what service provides the account, what mechanism of alerting will be used, or what the limitations are for a PowerShell solution in your environment.