Powershell - 用于更改 AD 用户到期日期的滚动脚本 [英] Powershell - Rolling script to change AD user expiration dates
问题描述
我希望为动态 AD 帐户到期日期修改编写脚本.我在下面编写的脚本大部分都可以工作,但不太像我想要的那样工作(我有点像 Powershell 菜鸟).
I'm looking to write a script for on-the-fly AD account expiration date amendments. The script I've written below mostly works, but does not quite work the way I want it to (I'm a bit of a Powershell noob).
我制作了一个滚动脚本,要求输入用户名,然后是新的到期日期,然后显示日期已更改为什么,然后重新开始.问题是,例如,如果我输入 05/07/2021,它将 AD 中的到期日期设置为 04/07/2021 午夜,但脚本报告日期更改为 05/07/2021.理想情况下,我希望将其更改为 05/07/2021 的午夜,但让脚本将更改日期报告为 05/07/2021,而不是 06/07/2021.
I've made a rolling script that asks for a username, then a new expiry date, then shows what the date has been changed to, then starts again. The problem is, that if I enter 05/07/2021, for example, it sets the expiration date in AD to midnight on 04/07/2021, but the script reports the date changed to 05/07/2021. Ideally, I want this to change it to midnight on 05/07/2021, but have the script report the changed date to 05/07/2021, not 06/07/2021.
我想通过这样做将输入日期向前滚动 24 小时:
I had the idea of rolling the input date forward 24 hours by doing this:
[datetime]$AD_User_date = Read-Host "Enter a new expiry date, in the format DD/MM/YYYY"
$AD_User_date_2 = $AD_User_date.AddHours(24)
Set-ADAccountExpiration -Identity $AD_User -DateTime $AD_User_date_2
但这会将日期格式更改为美国,因此输入 05/07/2021 会将帐户到期日期更改为 07/05/2021.这是一个无赖.
But this changes the date format to US, so inputting 05/07/2021 changes the account expiration date changes to 07/05/2021. Which is a bummer.
到目前为止我拥有的代码非常基本,我可能缺少一个明显的方法,请查看并告诉我我哪里出错了:
The code I have so far is super basic, I'm probably missing an obvious way of doing this, please have a look and let me know where I've gone wrong:
Clear-Host
$continue = $true
while ($continue){
write-host "AD Account Date Changer"
$AD_User = Read-Host "Enter username"
$AD_User = $AD_User.Trim()
$AD_User_date = Read-Host "Enter a new expiry date, in the format DD/MM/YYYY"
Set-ADAccountExpiration -Identity $AD_User -DateTime $AD_User_date
$AD_User_date_collect = Get-ADUser -Identity $AD_User -Properties AccountExpirationDate | select -ExpandProperty AccountExpirationDate
$AD_User_date_collect_2 = $AD_User_date_collect.ToString("dd-MM-yyyy")
Write-Host "New account expiration date is $AD_User_date_collect_2"
Write-Host ""
}
提前致谢.
推荐答案
您可能没有遇到您认为的问题.
You may not have the problem you think you have.
如果你这样做:
Set-ADAccountExpiration -Identity $AD_User -DateTime '05/07/2021'
然后
Get-ADUser -Identity $AD_User -Properties AccountExpirationDate
到期日期将显示为 05/07/2021 00:00:00,即 5 日的第一天.如果您查看 AD 用户和计算机,它会显示 04/07/2021,但是标题会清楚地显示结束:",以表明帐户将在 4 日全天处于活动状态:
The expiration date will show as 05/07/2021 00:00:00, i.e the very start of the day of the 5th. If you look in AD Users and Computers it will show 04/07/2021, however the caption clearly shows "End of:", to make it clear that the account will be active throughout the day of the 4th:
说了这么多,这里是您的脚本,带有一些日期验证:
All of that said, here is your script with a bit of date validation:
Clear-Host
$continue = $true
while ($continue){
write-host "AD Account Date Changer"
$AD_User = Read-Host "Enter username"
$AD_User = $AD_User.Trim()
$AD_User_date = new-object datetime
do{
$Entered_Date= Read-Host "Enter a new expiry date, in the format DD/MM/YYYY"
$result = [datetime]::tryparseexact($Entered_Date,'dd/MM/yyyy',[cultureinfo]("en-GB"), [globalization.datetimestyles]("None"),[ref]$AD_User_date)
if(!$result){write-host 'Invalid date entered!'}
}until($result)
try{
Set-ADAccountExpiration -Identity $AD_User -DateTime $AD_User_date.AddHours(24)
Write-Host "New account expiration date is the end of $(($AD_User_date).toString('dd/MM/yyyy'))"
}catch{
Write-Host "Unable to set account expiry: $_"
}
Write-Host ""
}
这篇关于Powershell - 用于更改 AD 用户到期日期的滚动脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!