【问题标题】:Powershell Foreach multiple arraysPowershell Foreach 多个数组
【发布时间】:2017-10-01 08:58:37
【问题描述】:

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

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

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

【问题讨论】:

    标签: arrays powershell foreach


    【解决方案1】:
    $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"
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-13
      • 2014-10-01
      • 2022-11-26
      • 1970-01-01
      • 1970-01-01
      • 2020-06-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多