[英]How can I modify this powershell code in Active Directory, to be able to change attributes when emply cells from csv file?
[英]How can I update more data rows in Active Directory from a csv file using Powershell?
我有一个包含人数据的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;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
在foreach-object
之后执行Set-ADUser
,这意味着您正在循环收集信息,逐行替换它,然后仅“设置”最后一位。
如果您希望Set-ADUser
对每一行都执行此操作,则需要将其放置在foreach
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.