使用Foreach的PowerShell Set-ADUser属性

时间:2013-05-27 08:10:44

标签: powershell foreach

我厌倦了谷歌搜索并不确定如何使这个条件有效。

userid.txt的内容:

user1
user2

phone.txt的内容:

phone1
phone2

代码:

$u = Get-Content(".\Desktop\Cubicle\userid.txt")
$p = Get-Content(".\Desktop\Cubicle\phone.txt")
foreach($uf in $u){foreach($pf in $p){Set-AdUser $uf -officephone $pf}}

错误:

Set-ADUser : replace
At line:1 char:49
+ foreach($uf in $u){foreach($pf in $p){Set-AdUser <<<<  $uf -officephone $pf}}
    + CategoryInfo          : InvalidOperation: (user1:ADUser) [Set-ADUser], ADInvalidOperationException
    + FullyQualifiedErrorId : replace,Microsoft.ActiveDirectory.Management.Commands.SetADUser

Set-ADUser : replace
At line:1 char:49
+ foreach($uf in $u){foreach($pf in $p){Set-AdUser <<<<  $uf -officephone $pf}}
    + CategoryInfo          : InvalidOperation: (user2:ADUser) [Set-ADUser], ADInvalidOperationException
    + FullyQualifiedErrorId : replace,Microsoft.ActiveDirectory.Management.Commands.SetADUser

如果将来我需要调用3个以上的文本文件,我该怎么说呢?

1 个答案:

答案 0 :(得分:2)

尝试这种方法。假设文件每行包含一个项目,并且两个文件具有相同的行长度。

$u = Get-Content ".\Desktop\Cubicle\userid.txt" 
$p = Get-Content ".\Desktop\Cubicle\phone.txt"

for($i=0; $i -lt $u.count; $i++)
{
    Set-AdUser $u[$i] -officephone $p[$i]
}