簡體   English   中英

Powershell Foreach 多個數組

[英]Powershell Foreach multiple arrays

我正在嘗試使用多個數組執行 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"
}

以上命令將每個用戶添加到每台計算機。 如何將單個用戶添加到他登錄的單台計算機? 我不能讓它工作,我還在學習,我沒有足夠的知識。 任何幫助表示贊賞。 先感謝您!

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM