0

我厌倦了谷歌和搜索,不知道如何让这个条件起作用。

内容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 个以上的文本文件,我该如何输入?

4

1 回答 1

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]
}
于 2013-05-27T08:21:47.630 回答