在运行PowerShell的计算机上,需要安装ActiveDirectory模块,它可以使PowerShell与Active Directory进行交互和操作。可以通过使用以下命令安装此模块:
Install-WindowsFeature RSAT-AD-PowerShell
创建名为“Extend-ADAccountExpiry.ps1”的新文件,并将以下代码copy到该文件中。
# Import ActiveDirectory module
Import-Module ActiveDirectory
# Read account name from user
$AccountName = Read-Host "Enter account name"
# Ask user to select method of expiry extension
$Choice = Read-Host "Select expiry extension period: 1 for 6 months, 2 for specific date"
# If user selects 6 months, add 6 months to current expiry date and update in Active Directory
if($Choice -eq 1) {
$User = Get-ADUser $AccountName -Properties AccountExpirationDate
$NewExpiryDate = $User.AccountExpirationDate.AddDays(180)
$User | Set-ADUser -AccountExpirationDate $NewExpiryDate
Write-Host "Expiry date of $AccountName has been extended by 6 months to $NewExpiryDate"
}
# If user selects specific date, read the date from user and update expiry date in Active Directory
if($Choice -eq 2) {
$ExpiryDate = Read-Host "Enter expiry date (format: MM/DD/YYYY)"
$User = Get-ADUser $AccountName -Properties AccountExpirationDate
$User | Set-ADUser -AccountExpirationDate (Get-Date $ExpiryDate)
Write-Host "Expiry date of $AccountName has been updated to $ExpiryDate"
}
在Powershell中,移动到脚本所在的目录,并执行以下命令来运行脚本:
.\Extend-