我正在嘗試使用 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"