我正在尝试使用 powershell 脚本和 file.csv 导入一些 AD 用户,但出现了以下错误:
New-AdUser : The specified account already exists
脚本如下:
Import-Module ActiveDirectory
$Users = Import-Csv -Delimiter ";" -Path ".\userslist.csv"
foreach ($User in $Users)
{
$OU = "OU=pfsense,DC=tech,DC=local"
$Password = $User.password
$Detailedname = $User.firstname + " " + $User.name
$UserFirstname = $User.Firstname
$FirstLetterFirstname = $UserFirstname.substring(0,1)
$SAM = $FirstLetterFirstname + $User.name
New-ADUser -Name $Detailedname -SamAccountName $SAM -UserPrincipalName $SAM -DisplayName $Detailedname -GivenName $user.firstname -Surname $user.name -AccountPassword (ConvertTo-SecureString $Password -AsPlainText -Force) -Enabled $true -Path $OU
}
以下是 CSV 文件:
Name;Firstname;Password
MILLE;Thierry;P@$$w0rd!1
MILLE;Vendelin;P@$$w0rd!2
MILLE;Blandine;P@$$w0rd!3
我应该怎样修复此问题?
答案1
仔细查看您的脚本,似乎您可能在输入 $FirstLetterFirstname 时手误。您需要在 $User**[dot]**Firstname.substring(0,1) 中添加点
$FirstLetterFirstname = $User.Firstname.substring(0,1)
您可能还想将您的域添加到 UserPrincipalName
-UserPrincipalName $SAM + "@EXAMPLE.com"