【问题标题】:Powershell: Item has already been added. Key in dictionary: '' Key being added: '' when update AD attributesPowershell:已添加项目。字典中的键:'' 正在添加的键:'' 更新 AD 属性时
【发布时间】:2020-10-05 06:03:57
【问题描述】:

以下 powershell 脚本应该从指定的 Excel 工作表更新 Active Directory 中的用户属性。但是我收到错误并且不确定为什么。似乎与哈希有关。

这是从指定的 Excel 表更新 AD 用户的脚本。

#Declare file path and sheet name
$credential=Get-Credential

[threading.thread]::CurrentThread.CurrentCulture = 'en-US'

$file = "filepath.xlsx"  
#Create an Excel.Application instance and open that file
$Excelobject = New-Object -ComObject Excel.Application
$Workbook = $Excelobject.Workbooks.Open($file) 
$sheetName = "Sheet1" 
$sheet = $Workbook.Worksheets.Item($sheetName)
#$objExcel.Visible=$true 

#Count max row
$rowMax = ($sheet.UsedRange.Rows).count
#Count max column
$colMax = ($sheet.UsedRange.Columns).count
$hash = @{}
$server = "127.0.0.1"

#Specify starting positions
$row,$col = 1,1
$updatedCount = 0

#loop for rows 
for ($i=1; $i -le $rowMax-1; $i++)
{

    #loop for columns
    for($c=0; $c -le $colMax-1; $c++)
    {
      #Get all columns values to a hash
      $hash += @{$sheet.Cells.Item($row,$col+$c).text =  $sheet.Cells.Item($row+$i,$col+$c).text}

    }

   #Create an object and assign hash keys as object property
   $Object = New-Object -TypeName PSObject -Property $hash

   #Get User via SamAccountname  
   $user = Get-ADUser -Filter "SamAccountName -eq '$($Object.UserName)'" -Server $server -Credential $credential

   #Set Users attribute with matched object attribute
   $user | Set-ADUser -Title $Object.Title `
              -PhysicalDeliveryOfficeName $Object.Office `
              -Description $Object.Description `

              # -DisplayName $Object.Displayname `

   #If you want to edit Object common name, you can remove enable two lines below.

   #$userguid = $user.ObjectGUID.Guid
   #$user | Rename-ADObject -NewName $Object.DisplayName -Server $server -Credential $credential

   $hash = @{}
   Write-Host $User.Name "- User attributes have been updated." -ForegroundColor Yellow
   Start-Sleep -s 1
   $updatedCount += 1

}

Write-Host $updatedCount "Users have been updated" -ForegroundColor Green

#close excel file
$Excelobject.quit() 

这是我在运行脚本时遇到的错误列表

Item has already been added. Key in dictionary: ''  Key being added: ''
At filepath.ps1:45 char:7
+       $hash += @{$sheet.Cells.Item($row,$col+$c).text =  $sheet.Cells ...
+       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (:) [], ArgumentException
    + FullyQualifiedErrorId : System.ArgumentException

Item has already been added. Key in dictionary: ''  Key being added: ''
At filepath.ps1:45 char:7
+       $hash += @{$sheet.Cells.Item($row,$col+$c).text =  $sheet.Cells ...
+       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (:) [], ArgumentException
    + FullyQualifiedErrorId : System.ArgumentException

New-Object : Cannot process argument because the value of argument "name" is not valid. Change the value of the "name" argument and run the operation again.
At filepath.ps1:51 char:14
+    $Object = New-Object -TypeName PSObject -Property $hash
+              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [New-Object], PSArgumentException
    + FullyQualifiedErrorId : Argument,Microsoft.PowerShell.Commands.NewObjectCommand


Set-ADUser : A parameter cannot be found that matches parameter name 'PhysicalDeliveryOfficeName'.
At filepath.ps1:58 char:15
+               -PhysicalDeliveryOfficeName $Object.Office `
+               ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Set-ADUser], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.ActiveDirectory.Management.Commands.SetADUser

【问题讨论】:

  • 第一个错误消息通常与它所说的完全一样。您已经在哈希表中添加了具有该确切键值的内容。您是否添加了诊断输出以向您确切地显示这些项目是什么?
  • 添加到@Lee_Dailey 的评论:第一条错误消息意味着您在 Excel 中的 UserName 列中有重复项。最后一条错误消息显示Set-ADUser 没有使用LDAP 属性名称physicalDeliveryOfficeName 作为参数名称。相反,您需要使用-Office $Object.Office
  • @Theo OK 我认为它正在尝试在 Excel 表中添加空行。
  • 不,因为您使用+= 添加到哈希表,如果已存在具有该名称的键,则会出现此错误。如果您不关心这一点并想在这种情况下覆盖现有条目,您可以使用此语法 $hash[$($sheet.Cells.Item($row,$col+$c).Text)] = $sheet.Cells ... ,但我会强烈建议先仔细检查您的输入 Excel AND 在测试时将 -WhatIf 开关添加到 Set-ADUser cmdlet。
  • 另外,我建议将您的 Excel 工作表(在重复数据删除之后)保存为 CSV 文件,因为这样可以更轻松地编写脚本,而无需从 Excel 工作表中(缓慢)读取的麻烦。跨度>

标签: powershell scripting active-directory


【解决方案1】:

我在我的 asp.net aspx 页面中遇到了这个问题。我发现原因如下:

任何 gridview 标头中的 Datakeynames 字段显示您可以从后面的代码访问的字段名称及其值。添加的名称列表不能有任何重复。我在这个列表中有一个重复的字段名称,一旦删除,错误就消失了。

我希望错误消息说:Duplicate Datakeynames - remove fieldname。相反,它谈论的是不能与 Datakeynames 直接相关的 Powershell。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-16
    • 2013-01-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多