简体   繁体   中英

Powershell Foreach multiple arrays

I am trying to performing a foreach loop with multiple arrays without success. I want to add users from a list as local administrators of computers they are logged Into invoking psexec. The property CustomComputername is an extensionAttribute representing the Computername the user is logged on.

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

Above command adds every user to every computer. How to add the single user to a single computer he's logged on? I can't get it working, I'm still learning and I have not enough knowledge. Any help is appreciated. Thank you in advance!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM