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