[英]Mass add users AD with Powershell
Import-Module ActiveDirectory
$file = "C:\Users\Administrator\Documents\UsersHR.csv"
$targetDN = "OU=HR,OU=NTTLab,DC=NTTLab,DC=internal"
$importedUsers = Import-Csv $file
foreach ($user in $importedUsers)
{
$Username = $User.Username
$Password = $User.Password
$Firstname = $User.Firstname
$Lastname = $User.Surname
$Name = $User.Firstname + $User.Lastname
$OU = "OU=HR,OU=NTTLab,DC=NTTLab,DC=internal"
$company = $User.company
$department = $User.department
$Password = $User.Password
New-ADUser -SamAccountName $Username -Name $Name -GivenName $Firstname -Surname $Lastname -Enabled $true -DisplayName "$Lastname, $Firstname" -Path $OU -Company $Company -Department $department -AccountPassword $Password -ChangePasswordAtLogon $true
}
I'm working on a VM of windows server 2016. I'm trying to add several users at once to the AD using PowerShell ISE, but I'm running into several errors about the name.我正在使用 Windows Server 2016 的 VM。我正在尝试使用 PowerShell ISE 将多个用户一次添加到 AD,但我遇到了几个关于名称的错误。 it's either not properly formed, empty or it's asking for it manually
它要么格式不正确,要么是空的,要么是手动要求的
You didn't say what it's complaining about, but I assume it's this:你没有说它在抱怨什么,但我认为是这样的:
$Username = $User.Username
...
New-ADUser -SamAccountName $Username
There are several User Naming Attributes in Active Directory. Active Directory 中有几个用户命名属性。 The
sAMAccountName
attribute is a short username. sAMAccountName
属性是一个简短的用户名。 It must be 20 characters or less.它必须是 20 个字符或更少。 Although the
@
character is technically allowed, it is usually never used.尽管
@
字符在技术上是允许的,但通常从不使用。 In fact, AD Users and Computers won't let you put an @
in it.事实上,AD Users and Computers 不会让你在其中输入
@
。
That "Username" you have in your file is a better fit for the userPrincipalName
attribute .您文件中的“用户名”更适合
userPrincipalName
属性。
But you will still have to figure something out for the sAMAccountName
.但是您仍然需要为
sAMAccountName
找出一些东西。 Our organization uses the last name (cropped at 18 characters) and first two letters of the first name.我们的组织使用姓氏(裁剪为 18 个字符)和名字的前两个字母。 That would look something like this:
这看起来像这样:
Import-Module ActiveDirectory
$file = "C:\Users\Administrator\Documents\UsersHR.csv"
$targetDN = "OU=HR,OU=NTTLab,DC=NTTLab,DC=internal"
$importedUsers = Import-Csv $file
foreach ($user in $importedUsers)
{
$SamAccountName = "$($User.Surname.Substring(0, [System.Math]::Min(18, $User.Surname.Length)))$($User.Firstname)"
$UserPrincipalName = $User.Username
$Password = $User.Password
$Firstname = $User.Firstname
$Lastname = $User.Surname
$Name = "$($User.Firstname) $($User.Surname)"
$OU = "OU=HR,OU=NTTLab,DC=NTTLab,DC=internal"
$company = $User.company
$department = $User.department
$Password = $User.Password
New-ADUser -SamAccountName $SamAccountName -UserPrincipalName $UserPrincipalName -Name $Name -GivenName $Firstname -Surname $Lastname -Enabled $true -DisplayName "$Lastname, $Firstname" -Path $OU -Company $Company -Department $department -AccountPassword $Password -ChangePasswordAtLogon $true
}
I also fixed how you defined $Name
, since it didn't have a space, and you were using $User.Lastname
instead of $User.Surname
.我还修复了您如何定义
$Name
,因为它没有空格,并且您使用的是$User.Lastname
而不是$User.Surname
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.