PowerShell 脚本:使用 Set-ADUser 命令添加用户377
在 Active Directory 中管理用户是系统管理员的常见任务。使用 PowerShell 脚本自动化此过程可以显着节省时间并提高准确性。本文将介绍如何使用 Set-ADUser 命令创建新用户。
安装 Active Directory 模块
PowerShell Active Directory 模块需要与 Active Directory 交互。如果尚未安装,请使用以下命令对其进行安装:```
Install-Module ActiveDirectory
```
连接到 Active Directory
连接到 Active Directory 以执行任务至关重要。使用以下命令连接到 Active Directory:```
Connect-ADDomainController
```
创建新用户
要使用 Set-ADUser 命令创建新用户,请使用以下语法:```
Set-ADUser -Name "Display Name" -GivenName "First Name" -Surname "Last Name" -UserPrincipalName "User Login" -SamAccountName "Short Name" -AccountPassword (ConvertTo-SecureString -AsPlainText "Password" -Force) -Enabled $true
```
其中:- -Name:要显示在 Active Directory 中的名称。
- -GivenName:用户的给定名。
- -Surname:用户的姓氏。
- -UserPrincipalName:用户的登录名。
- -SamAccountName:用户的短名。
- -AccountPassword:将用户的密码转换为安全字符串。
- -Enabled $true:启用用户帐户。
示例脚本
以下是一个示例脚本,演示如何使用 Set-ADUser 命令创建新用户:```
$Name = "John Smith"
$GivenName = "John"
$Surname = "Smith"
$UserPrincipalName = "jsmith@"
$SamAccountName = "jsmith"
$Password = "MyPassword123"
Set-ADUser -Name $Name -GivenName $GivenName -Surname $Surname -UserPrincipalName $UserPrincipalName -SamAccountName $SamAccountName -AccountPassword (ConvertTo-SecureString -AsPlainText $Password -Force) -Enabled $true
```
处理错误
在自动化脚本中处理错误非常重要。要使用 Set-ADUser 命令捕获错误,请使用以下 try-catch 块:```
try {
# 执行 Set-ADUser 命令
} catch [Exception] {
# 处理错误
}
```
使用 PowerShell 脚本和 Set-ADUser 命令创建新用户是一种高效且准确的方法。通过自动化流程,系统管理员可以节省时间和提高生产力。通过遵循本文所述的步骤,您可以轻松地添加用户并管理 Active Directory。
2024-12-03

客户脚本语言详解:深入理解浏览器端的编程世界
https://jb123.cn/jiaobenyuyan/65389.html

快速掌握脚本语言:学习策略与技巧详解
https://jb123.cn/jiaobenyuyan/65388.html

Perl字体颜色控制详解:从基础语法到高级技巧
https://jb123.cn/perl/65387.html

Python趣味编程:玩转京东自营商品数据
https://jb123.cn/python/65386.html

JavaScript 版本详解及兼容性策略
https://jb123.cn/javascript/65385.html
热门文章

如何使用 PowerShell 以管理员权限运行脚本
https://jb123.cn/powershell/5326.html

使用 boost 轻松调用 PowerShell 脚本
https://jb123.cn/powershell/3480.html

探索 PowerShell 脚本编写的奥妙
https://jb123.cn/powershell/2933.html

如何在 PowerShell 中运行脚本
https://jb123.cn/powershell/2580.html

Powershell 脚本选项命令:深入理解 Get-Help
https://jb123.cn/powershell/2088.html