简体   繁体   English

如何使用Powershell从csv文件更新Active Directory中的更多数据行?

[英]How can I update more data rows in Active Directory from a csv file using Powershell?

I have a csv file with people data. 我有一个包含人数据的csv文件。 One row per person. 每人一行。 I want to update Active Directory with the data using powershell, but only the first row is updated in Active Directory. 我想使用Powershell用数据更新Active Directory,但是在Active Directory中仅更新第一行。

How can I modify the code to update all the row values? 如何修改代码以更新所有行值?

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

The csv file: csv文件:

SamAccountName;surname;givenname;displayname;Job Title;Department;Manager;HomePhone;MobilePhone
test1;Test;1;Test 1;;;;;
test2;Test;2;Test 2;something;anything;test1;136 / 8022;-1079

Your doing the Set-ADUser after the foreach-object , this means you're looping through collecting the information, replacing it line by line, then only 'setting' the final bit. foreach-object之后执行Set-ADUser ,这意味着您正在循环收集信息,逐行替换它,然后仅“设置”最后一位。

Set-ADUser needs to be in the foreach if you want it to do it for each line. 如果您希望Set-ADUser对每一行都执行此操作,则需要将其放置在foreach

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何在Active Directory中修改此powershell代码,以便在从CSV文件中清空单元格时能够更改属性? - How can I modify this powershell code in Active Directory, to be able to change attributes when emply cells from csv file? 如何使用Powershell在CSV文件中添加数据,插入行和删除列,然后保存? - How can I add data, insert rows and delete columns in a CSV file using powershell and then save it? 如何使用PowerShell将用户从CSV文件添加到Active Directory(AD)和Exchange中? - How do you add users from a CSV file to Active Directory (AD) and Exchange using PowerShell? 如何将这些 Active Directory 帐户操作记录到 CSV 文件中? - How can I log these Active Directory account operations to a CSV file? 如何将以下 CSV 文件转换为 Active Directory 用户 - How can I turn the following CSV file into Active Directory Users 如何从CSV列中读取用户名并将Active Directory中的相关数据添加到他们的行中? - How can I read in user names from a CSV column and add relevant data from Active Directory to their row? Powershell导出包含活动目录安全组数据的csv文件 - Powershell exports csv file containing active directory security group data 在Powershell中读取CSV文件以从Active Directory获取信息 - Reading CSV file in powershell to get info from Active directory 使用 PowerShell 从.csv 更新 Active Directory 管理器属性 - Updating Active Directory Manager Attribute from .csv Using PowerShell 如何在 PowerShell 中从 csv 查找 Active Directory 组名称? - How to look for Active Directory group name from csv in PowerShell?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM