-1
此腳本今天在測試時工作得很好。我做了一些更改並開始出現錯誤。然後我回到了我從互聯網上獲得的非常原始的劇本,現在甚至都不起作用。錯誤是:將多個用戶添加到AD
Exception calling "SetInfo" with "0" argument(s): "A constraint violation occurred." At C:\Scripts\CreateTest2.ps1:51 char:2 + $LABUser.SetInfo() + ~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : CatchFromBaseAdapterMethodInvokeTI Exception calling "Invoke" with "2" argument(s): "There is no such object on the server." At C:\Scripts\CreateTest2.ps1:55 char:2 + $LABUser.psbase.invoke("setPassword", $Pwrd) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : DotNetMethodTargetInvocation Exception calling "InvokeSet" with "2" argument(s): "The directory property cannot be found in the cache" At C:\Scripts\CreateTest2.ps1:56 char:2 + $LABUser.psbase.invokeSet("AccountDisabled", $false) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : DotNetMethodTargetInvocation Exception calling "CommitChanges" with "0" argument(s): "A constraint violation occurred." At C:\Scripts\CreateTest2.ps1:57 char:2 + $LABUser.psbase.CommitChanges() + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : DotNetMethodException
這是腳本。我究竟做錯了什麼?
function Select-FileDialog {
param(
[string]$Title,
[string]$Directory,
[string]$Filter = "CSV Files(*.csv)|*.csv"
)
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null
$objForm = New-Object System.Windows.Forms.OpenFileDialog
$objForm.InitialDirectory = $Directory
$objForm.Filter = $Filter
$objForm.Title = $Title
$objForm.ShowHelp = $true
$Show = $objForm.ShowDialog()
if ($Show -eq "OK") {
return $objForm.FileName
} else {
exit
}
}
$FileName = Select-FileDialog -Title "Import an CSV file" -Directory "c:\"
$SelectOU = "OU=Test2,OU=Users,OU=Domain Controllers"
$domain = [System.DirectoryServices.ActiveDirectory.Domain]::GetComputerDomain()
$DomainDN = (([System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest()).Domains | ? {$_.Name -eq $domain}).GetDirectoryEntry().distinguishedName
$final = "LDAP://$DomainDN"
$DomainPath = [ADSI]"$final"
$UserInformation = Import-Csv $FileName
$OUPath = "LDAP://$SelectOU,$DomainDN"
$UserPath = [ADSI]"$OUPath"
foreach ($User in $UserInformation) {
$CN = $User.samAccountName
$SN = $User.Surname
$Given = $User.givenName
$samAccountName = $User.samAccountName
$Display = $User.DisplayName
$LABUser = $UserPath.Create("User", "CN=$CN")
Write-Host "Please Wait..."
$LABUser.Put("samAccountName", $samAccountName)
$LABUser.Put("sn", $SN)
$LABUser.Put("givenName", $Given)
$LABUser.Put("displayName", $Display)
$LABUser.Put("userPrincipalName", "[email protected]$domain")
$LABUser.SetInfo()
$Pwrd = $User.Password
$LABUser.psbase.invoke("setPassword", $Pwrd)
$LABUser.psbase.invokeSet("AccountDisabled", $false)
$LABUser.psbase.CommitChanges()
}
Write-Host "Script Completed"
對不起,搞砸了格式 –
所以,你問什麼,到底?你到目前爲止做了哪些故障排除?您是否搜索了錯誤消息的含義? (這個想法是至少自己執行一些敷衍故障排除,而不是做一個「代碼轉儲」,然後期待其他人爲你調試和修復它。) –
違反約束,第一個也是最重要的錯誤,重新要求目錄做一些不想做的事情。複製sAMAccountName,複製userPrincipalName,複製distinguishedName或相對distinguishedName等。 –