2017-10-01 65 views
-1

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

$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" 
} 

上述命令将每个用户添加到每台计算机。 如何将单个用户添加到他登录的单台计算机上? 我无法工作,我仍然在学习,而且我没有足够的知识。任何帮助表示赞赏。先谢谢你!

回答

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" 
}