在Active Directory(AD)中使用CSV文件批量创建用户可以通过PowerShell脚本来实现。以下是一个基本的步骤指南:
一、准备工作
SamAccountName
(用户的登录名)UserPrincipalName
(用户的完整登录名,通常是SamAccountName@域名
)Name
(用户的显示名称)GivenName
(用户的名)Surname
(用户的姓)Password
(用户的密码,需要符合AD的密码策略)Enabled
(是否启用账户,通常为True
或False
)。SamAccountName,UserPrincipalName,Name,GivenName,Surname,Password,Enabled
user1,user1@example.com,User One,User,One,P@ssw0rd,True
user2,user2@example.com,User Two,User,Two,P@ssw0rd,True
二、编写PowerShell脚本
Import - Module ActiveDirectory
$csvPath = "C:\path\to\your\users.csv" $users = Import - Csv - Path $csvPath foreach ($user in $users) { $samAccountName = $user.SamAccountName $userPrincipalName = $user.UserPrincipalName $name = $user.Name $givenName = $user.GivenName $surname = $user.Surname $password = ConvertTo - SecureString $user.Password - AsPlainText - Force New - ADUser - SamAccountName $samAccountName - UserPrincipalName $userPrincipalName - Name $name - GivenName $givenName - Surname $surname - PasswordNeverExpires $true - AccountPassword $password - Enabled $user.Enabled }
Import - Csv
命令读取CSV文件内容到一个变量中。foreach
循环遍历每个用户记录,在循环内部,提取各个属性的值,并使用New - ADUser
命令创建用户。其中,ConvertTo - SecureString
用于将明文密码转换为安全字符串,以满足AD对密码的要求。三、执行脚本
.ps1
文件,然后在PowerShell中导航到该文件所在的目录并执行它。请注意:
领取专属 10元无门槛券
手把手带您无忧上云