每 85 天提醒一次操作
Alert for an action every 85 days
我正在编写一个脚本,显示距离我们应该更改密码还剩多少天。问题是现在,我们大约需要 11 天才能更改密码。我正在为编写脚本的逻辑而苦苦挣扎。 85 天倒计时的开始是 2021 年 5 月 16 日。接下来的 85 天从 2021 年 8 月 9 日开始。我希望在 85 天后重新设置日期。老实说,我什至不确定如何开始。也许是 returns 85 - $currentDate 然后在 85 之后重置的 for 循环,但我不确定如何让它在 85 天的中间今天开始。
在您的脚本中,您可以创建 May 16, 2021
的开始日期并根据需要将该日期增加 85 天:
# Original Start Date at 12:00 AM
Date = [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 Date) {
# increments Date by 85 days
Date = Date.AddDays(85)
}
# check when there are 0 days remaining
if ($Current.Date -eq Date) {
# Send alert here for 0 days remaining
}
# output how many days remain
'{0} days remaining until password expiration' -f (Date - $current.Date).Days
除非您添加该逻辑,否则代码不会知道密码是否已更新。它只会假设您每次都等到第 85 天重置它。要添加更多智能,您需要执行以下操作之一:
- 添加测试密码已重置的逻辑
- 让密码重置更新可从 PowerShell 读取的帐户属性。可以使用该日期代替今天。
- 使用密码更改日期更新一些其他工件,例如数据库 table 或文件。然后在每次运行时读取脚本。
有很多替代方法可以使这项工作成功。截至目前,我们不知道提供该帐户的服务是什么,将使用什么警报机制,或者您的环境中 PowerShell 解决方案的限制是什么。
我正在编写一个脚本,显示距离我们应该更改密码还剩多少天。问题是现在,我们大约需要 11 天才能更改密码。我正在为编写脚本的逻辑而苦苦挣扎。 85 天倒计时的开始是 2021 年 5 月 16 日。接下来的 85 天从 2021 年 8 月 9 日开始。我希望在 85 天后重新设置日期。老实说,我什至不确定如何开始。也许是 returns 85 - $currentDate 然后在 85 之后重置的 for 循环,但我不确定如何让它在 85 天的中间今天开始。
在您的脚本中,您可以创建 May 16, 2021
的开始日期并根据需要将该日期增加 85 天:
# Original Start Date at 12:00 AM
Date = [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 Date) {
# increments Date by 85 days
Date = Date.AddDays(85)
}
# check when there are 0 days remaining
if ($Current.Date -eq Date) {
# Send alert here for 0 days remaining
}
# output how many days remain
'{0} days remaining until password expiration' -f (Date - $current.Date).Days
除非您添加该逻辑,否则代码不会知道密码是否已更新。它只会假设您每次都等到第 85 天重置它。要添加更多智能,您需要执行以下操作之一:
- 添加测试密码已重置的逻辑
- 让密码重置更新可从 PowerShell 读取的帐户属性。可以使用该日期代替今天。
- 使用密码更改日期更新一些其他工件,例如数据库 table 或文件。然后在每次运行时读取脚本。
有很多替代方法可以使这项工作成功。截至目前,我们不知道提供该帐户的服务是什么,将使用什么警报机制,或者您的环境中 PowerShell 解决方案的限制是什么。