Powershell:项目已添加。键入字典:”添加密钥:”更新AD属性时

时间:2020-06-15 18:37:23

标签: powershell scripting active-directory

下面的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

1 个答案:

答案 0 :(得分:1)

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

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

我希望错误消息说:重复数据键名 - 删除字段名。相反,它谈论的是 Powershell,它不能与 Datakeynames 直接相关。

相关问题