编写一个PowerShell脚本,延长AD帐户到期日期并选择延长期限为6个月或输入日期。
创始人
2024-12-07 05:00:18
0
  1. 安装ActiveDirectory 模块

在运行PowerShell的计算机上,需要安装ActiveDirectory模块,它可以使PowerShell与Active Directory进行交互和操作。可以通过使用以下命令安装此模块:

Install-WindowsFeature RSAT-AD-PowerShell
  1. 编写脚本

创建名为“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"
}
  1. 运行脚本

在Powershell中,移动到脚本所在的目录,并执行以下命令来运行脚本:

.\Extend-

相关内容

热门资讯

Android Recycle... 要在Android RecyclerView中实现滑动卡片效果,可以按照以下步骤进行操作:首先,在项...
安装apache-beam==... 出现此错误可能是因为用户的Python版本太低,而apache-beam==2.34.0需要更高的P...
Android - 无法确定任... 这个错误通常发生在Android项目中,表示编译Debug版本的Java代码时出现了依赖关系问题。下...
Android - NDK 预... 在Android NDK的构建过程中,LOCAL_SRC_FILES只能包含一个项目。如果需要在ND...
Akka生成Actor问题 在Akka框架中,可以使用ActorSystem对象生成Actor。但是,当我们在Actor类中尝试...
Agora-RTC-React... 出现这个错误原因是因为在 React 组件中使用,import AgoraRTC from “ago...
Alertmanager在pr... 首先,在Prometheus配置文件中,确保Alertmanager URL已正确配置。例如:ale...
Aksnginxdomainb... 在AKS集群中,可以使用Nginx代理服务器实现根据域名进行路由。以下是具体步骤:部署Nginx i...
AddSingleton在.N... 在C#中创建Singleton对象通常是通过私有构造函数和静态属性来实现,例如:public cla...
Alertmanager中的基... Alertmanager中可以使用repeat_interval选项指定在一个告警重复发送前必须等待...