【问题标题】:How can I update more data rows in Active Directory from a csv file using Powershell?如何使用 Powershell 从 csv 文件更新 Active Directory 中的更多数据行?
【发布时间】:2023-03-29 07:35:01
【问题描述】:

我有一个包含人员数据的 csv 文件。每人一排。 我想使用 powershell 用数据更新 Active Directory,但 Active Directory 中只更新了第一行。

如何修改代码以更新所有行值?

Import-Module ActiveDirectory
Import-Csv "C:\code.csv" -Delimiter ';' -Encoding "UTF8" |
ForEach-Object
{
    $params = @{Identity = $_.SamAccountName}
    if (-not [string]::IsNullOrWhiteSpace($_.Surname))
    {
        $params.Surname = $_.Surname
    }
    if (-not [string]::IsNullOrWhiteSpace($_.Givenname))
    {
        $params.Givenname = $_.Givenname
    }
    if (-not [string]::IsNullOrWhiteSpace($_.DisplayName))
    {
        $params.DisplayName = $_.DisplayName
    }
    if (-not [string]::IsNullOrWhiteSpace($_.Title))
    {
        $params.Title = $_.Title
    }
    if (-not [string]::IsNullOrWhiteSpace($_.Department))
    {
         $params.Department = $_.Department
    }
    if (-not [string]::IsNullOrWhiteSpace($_.Manager)) 
   {
        $params.Manager = $_.Manager
   }
   if (-not [string]::IsNullOrWhiteSpace($_.OfficePhone)) 
   {
       $params.OfficePhone = $_.OfficePhone
   }
   if (-not [string]::IsNullOrWhiteSpace($_.MobilePhone)) 
   {
        $params.MobilePhone = $_.MobilePhone
   }
}
Set-ADUser @params

csv 文件:

SamAccountName;姓;给定姓名;显示名称;职位;部门;经理;家庭电话;手机 测试1;测试;1;测试1;;;;; test2;Test;2;Test 2;某事;任何事;test1;136 / 8022;-1079

【问题讨论】:

    标签: powershell csv active-directory


    【解决方案1】:

    您在foreach-object 之后执行Set-ADUser,这意味着您正在循环收集信息,逐行替换它,然后只“设置”最后一位。

    Set-ADUser 需要在 foreach 中,如果您希望它对每一行都执行此操作。

    【讨论】:

    • 太棒了!感谢帮助!
    猜你喜欢
    • 2021-02-18
    • 2023-03-26
    • 1970-01-01
    • 2016-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多