Powershell 向广告添加多个用户

Powershell 向广告添加多个用户,powershell,scripting,active-directory,Powershell,Scripting,Active Directory,这个脚本在今天的测试中运行得很好。我做了一些修改,开始出错。然后我又回到了我从互联网上得到的最原始的剧本,现在连它都不起作用了。错误是: Exception calling "SetInfo" with "0" argument(s): "A constraint violation occurred." At C:\Scripts\CreateTest2.ps1:51 char:2 + $LABUser.SetInfo() + ~~~~~~~~~~~~~~~~~~ +

这个脚本在今天的测试中运行得很好。我做了一些修改,开始出错。然后我又回到了我从互联网上得到的最原始的剧本,现在连它都不起作用了。错误是:

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
你能试着从中删除引号吗

1) 实验室用户分配,即CN=$CN

2) 从该行中删除CN=


请一个接一个地试一下,我希望你能得到答案。

很抱歉,格式太乱了。那么,你到底在问什么?到目前为止,您做了哪些故障排除?您是否搜索了错误消息的含义?(想法是至少自己执行一些敷衍性的故障排除,而不是执行“代码转储”,然后期望其他人为您调试和修复。)约束冲突,第一个也是最重要的错误,意味着您要求目录执行它不想做的事。复制sAMAccountName、复制userPrincipalName、复制DifferentizedName或相对DifferentizedName等等。我已经完成了自己的故障排除。我回到了一直有效的原始代码,从广告中删除了所有用户,但仍然得到了错误。我将尝试使用一个新名称列表,但它不应该重复任何内容,因为我已删除了所有用户。因此,本质上,是的:您正在要求其他人为您排除故障、调试/修复/重写代码。我建议您发布一个最小的示例(请参阅),该示例没有任何GUI粗糙或非必要的代码,以便其他人可以轻松地重现该问题。CN=在该上下文中是必需的,Create的参数必须包含对象类和相对的可分辨名称。基本方法(通过ADSI在iADSContainer上显示)在此处有文档记录,并通过DirectoryEntry对象以简化形式显示:
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", "$samAccountName@$domain")
  $LABUser.SetInfo()

  $Pwrd = $User.Password

  $LABUser.psbase.invoke("setPassword", $Pwrd)
  $LABUser.psbase.invokeSet("AccountDisabled", $false)
  $LABUser.psbase.CommitChanges()
}
Write-Host "Script Completed"