首页 > 解决方案 > 用于更新现有用户的 Active Directory 属性的 Powershell 脚本

问题描述

我有一个包含多个列的源 CSV 文件,我想将其中一些但不是所有列更新到 AD 中的匹配用户中。

# Import AD Module             
Import-Module ActiveDirectory            

# Import CSV into variable $userscsv            
#$userscsv = import-csv             
$users = Import-Csv -Path 'C:\Scripts\ADUPLOADtest2.csv'           
# Loop through CSV and update users if the exist in CSV file            

foreach ($user in $users)
{            
    #Search in specified OU and Update existing attributes            
    Get-ADUser -Filter "SamAccountName -eq '$($user.Username)'" -Properties * |            
        Set-ADUser -employeeNumber $($user.Emp_Code) -extensionAttribute4 $($user.Cost_Centre) -title $($user.Role) -Department $($user.Work_Area) 
 }

源 CSV 文件示例:

Emp Code,Username,Surname,Manager,First name,Department,Division,Section,Team,Role,Work Area,Cost Centre
82644,aah,Haven,Peter Jones,April,Executive,Community Wellbeing,General,General,Community Wellbeing Support Officer,Community Wellbeing,50016001

将上面的代码与示例 CSV 一起使用,我目前在 extensionAttribute4 字段更新中收到以下错误:

Powershell 错误

任何建议将不胜感激。

标签: powershell

解决方案


正如错误所说,该参数不存在。你有没有尝试过 - Set-ADUser -Add @{attributename="TheRequiredValue"}


推荐阅读