[英]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.