【发布时间】: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