2017-10-01 119 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" 
}