Powershell Foreach多个数组

时间:2017-10-01 08:58:37

标签: arrays powershell foreach

我正在尝试使用多个数组执行foreach循环但没有成功。 我想将列表中的用户添加为他们登录到调用psexec的计算机的本地管理员。 CustomComputername属性是一个extensionAttribute,表示用户登录的Computername。

$array1= get-content "C:\list.txt"
$array2= foreach ($u in $array1)
{get-aduser -filter {samaccountname -eq $user} -Properties CustomComputername |
Select -expandproperty CustomComputername}

foreach ($Computer in $array2){
foreach ($u in $array 1)      {
Invoke-PsExec -ComputerName $Computer -Command "net localgroup administrators $u /add"
}

上面的命令将每个用户添加到每台计算机。 如何将单个用户添加到他登录的单台计算机上? 我不能让它工作,我还在学习,我没有足够的知识。任何帮助表示赞赏。提前谢谢!

1 个答案:

答案 0 :(得分:0)

$array1= get-content "C:\list.txt"
$array2= foreach ($u in $array1)
{get-aduser -filter {samaccountname -eq $user} -Properties CustomComputername |
Select -expandproperty CustomComputername | %{@($user,$_)}}

foreach ($info in $array2){
Invoke-PsExec -ComputerName $info[1] -Command "net localgroup administrators $($info[0]) /add"
}