2016-08-08 119 views
-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" 
+0

對不起,搞砸了格式 –

+1

所以,你問什麼,到底?你到目前爲止做了哪些故障排除?您是否搜索了錯誤消息的含義? (這個想法是至少自己執行一些敷衍故障排除,而不是做一個「代碼轉儲」,然後期待其他人爲你調試和修復它。) –

+2

違反約束,第一個也是最重要的錯誤,重新要求目錄做一些不想做的事情。複製sAMAccountName,複製userPrincipalName,複製distinguishedName或相對distinguishedName等。 –

回答

-1

能不能請你從

1)LABUser分配,即CN = $ CN卸下行情。

2)從該行刪除CN =。

嘗試一個一個我希望你能得到你的答案。

+0

CN =在該上下文中是必需的,Create的參數必須包含對象類和相對可分辨名稱。底層方法(通過ADSI在iADSContainer上展示)在此處記錄並通過DirectoryEntry對象以簡化形式呈現:https://msdn.microsoft.com/en-us/library/aa705987(v=vs.85)。 ASPX –

相關問題