Powershell 同时处理2个列表

Powershell 同时处理2个列表,powershell,foreach,Powershell,Foreach,我正在编写powershell脚本,该脚本在远程计算机上搜索特定文件并将其导入(保存)到xx.txt文件,如果扫描正常,则发送带有附加文件的电子邮件,如果扫描不正常,则只发送带有错误消息的电子邮件。(我在域中有70台计算机) 现在我要做的是把计算机名和用户名连接起来。(我对powershell脚本还很陌生) 以下是脚本: $people = Get-Content "D:\Script\people.txt" # list of people. One user per line. $compu

我正在编写powershell脚本,该脚本在远程计算机上搜索特定文件并将其导入(保存)到xx.txt文件,如果扫描正常,则发送带有附加文件的电子邮件,如果扫描不正常,则只发送带有错误消息的电子邮件。(我在域中有70台计算机)

现在我要做的是把计算机名和用户名连接起来。(我对powershell脚本还很陌生)

以下是脚本:

$people = Get-Content "D:\Script\people.txt" # list of people. One user per line.
$computers = Get-Content "D:\Script\list.txt" # list of computers is stored in list.txt file. One coputer name per line
$day = (get-date).DayOfWeek # day of week
$time = get-date -Format "HH:mm:ss" # time
$date = get-date -Format  "dd.MM.yyyy" # date

foreach ($a in $people){}    

foreach ($i in $computers)

    {$i + $a + "`n" + 
                "================================================="
                "|         " + $time + " - " + $day + " - " + $date+"        |"           # 14:57:54 - Friday - 17.10.2014
                "================================================="
                "|       Testing connection on computer $i    |"
                "================================================="

                $test_connect = Test-Connection -ComputerName $i -Count 2 -ErrorAction SilentlyContinue  # Ping computer with 2 counts and don't dispay errors

             if ($test_connect -ne $null) # if ping OK, continue with searching for files
                {   
                    "================================================="
                    "|          Testing connection PASSED            |"
                    "|       Scan started on $i at $a             |"
                    "================================================="

                    $RemoteSB = $executioncontext.invokecommand.NewScriptBlock("get-childitem c:\*,d:\* -include *.xls,*.xlsx,*.mobi,*.avi,*.mp3,*.mp4,*.csv,*.aif,*.iff,*.mid,*.ra,*.wav,*.wma,*.mov,*.mpg,*.rm,*.wmv,*.epub -exclude *.xlm -recurse")
                    invoke-command -computername $i -scriptblock $RemoteSB -ErrorAction SilentlyContinue > X:\$i.txt

                $smtpTo = "example@example.com"
                "================================================="
                "|              Search done on $i             |"
                "|          Continuing to next computer          |"
                "|      email report sent to $smtpTo      |"
                "|            !!!! SCAN COMPLETE !!!             |"
                "================================================="
                "                              "
                $file = "X:\$i.txt" # file name list of computers and their destination PATH$
                $smtpServer = "smtp.gmail.com" # enter your SMTP server
                $smtpFrom = "example@example.com" # send email FROM address
                $smtpTo = "example@example.com" # send email TO address
                $messageSubject = "Scan Done for computer $i on $date" # SUBJECT line
                $messagebody = "Scan for computer $i is done on $day $date at $time, continuing to next one. This computer belongs to $a." #email BODY text
                Send-MailMessage -SmtpServer $smtpServer -To $smtpTo -From $smtpFrom -Subject $messageSubject -Body $messagebody -attachment $file # Sendmail command

                }

            else # if PING fails send email with error
                {
                    "================================================="
                    "|                !!!! ERROR !!!                 |"
                    "================================================="
                    "================================================="
                    "|       Testing connection on $i FAILED      |"
                    "================================================="
                    "  "

                $smtpServer = "smtp.gmail.com" # enter your SMTP server
                $smtpFrom = "example@example.com" # send email FROM address
                $smtpTo = "example@example.com" # send email TO address
                $messageSubjectError = "!!!ERROR!!! Scan for computer $i failed" # SUBJECT line
                $messagebodyError =  "Error Scan for computer $i at $day $date on $time, continuing to next one." # email BODY text 
                Send-MailMessage -SmtpServer $smtpServer -To $smtpTo -From $smtpFrom -Subject $messageSubjectError -Body $messagebodyError # Sendmail command

                }
      }
如果我不把foreach($a in$people){}放在第一位的话,这个脚本工作得非常好 我认为($a in$people){XXXXXXXXX}或类似的东西缺少了一些东西,但我仍然不明白是什么

如果有人能帮忙,我将不胜感激

问候

内文戈托瓦克

更新:

尝试使用下面的答案,但不起作用,或者我把它放错了行。 脚本的输出如下所示。如果你检查一下,你会发现“奥古斯丁”这个名字总是重复。相反,它应该像计算机名一样更改

  PS C:\Users\Administrator> D:\Script\run3.ps1
  c04 - augustin
  =================================================
  |         17:44:29 - Friday - 17.10.2014        |
  =================================================
  |       Testing connection on computer c04    |
  =================================================
  =================================================
  |                !!!! ERROR !!!                 |
  =================================================
  =================================================
  |Testing connection on c4 for augustin FAILED   |
  =================================================

  C37 - augustin
  =================================================
  |         17:44:29 - Friday - 17.10.2014        |
  =================================================
  |       Testing connection on computer C37      |
  =================================================
  =================================================
  |                !!!! ERROR !!!                 |
  =================================================
  =================================================
  |Testing connection on C37 for augustin FAILED  |
  =================================================

  C51 - augustin
  =================================================
  |         17:44:29 - Friday - 17.10.2014        |
  =================================================
  |       Testing connection on computer C51      |
  =================================================
  =================================================
  |                !!!! ERROR !!!                 |
  =================================================
  =================================================
  |Testing connection on C51 for augustin FAILED  |
  =================================================
@马特
我已经像你说的那样用括号括起来了,但它仍然重复username:(如果username在people.txt文件中的firstname.lastname中间有点是不是有问题?

我还没有检查所有代码,但有一个问题我看到这行

foreach ($a in $people){}
虽然这会起作用,但分配给它的表达式是空的
{}
。您需要将右大括号移动到脚本的末尾

foreach ($a in $people){}    
foreach ($i in $computers)

    {$i + $a + "`n" + 
                "================================================="
                "|         " + $time + " - " + $day + " - " + $date+"        |"          
                "================================================="
                #truncated to save space            
    }
应该是

foreach ($a in $people){    
foreach ($i in $computers)

    {$i + $a + "`n" + 
                "================================================="
                "|         " + $time + " - " + $day + " - " + $date+"        |"          
                "================================================="
                #truncated to save space            
    }
}
您所听到的逻辑将为每台计算机处理每个人
$a
$i

从问题编辑更新

我认为您需要检查每台计算机及其相应的用户,对吗?如果是这种情况,您可以通过单个索引循环执行。有更好的方法来执行此操作,但这将与您当前的代码很好地融合

For($index=0;$index -lt $people.Count; $index++){
    $a = $people[$index]
    $i = $computers[$index]

    $i + $a + "`n" +
    #... Process Stuff Here

}

用它来代替当前代码中的两个for循环。

我还没有检查所有代码,但有一个问题我看到这行是否正确

foreach ($a in $people){}
虽然这会起作用,但分配给它的表达式是空的
{}
。您需要将右大括号移动到脚本的末尾

foreach ($a in $people){}    
foreach ($i in $computers)

    {$i + $a + "`n" + 
                "================================================="
                "|         " + $time + " - " + $day + " - " + $date+"        |"          
                "================================================="
                #truncated to save space            
    }
应该是

foreach ($a in $people){    
foreach ($i in $computers)

    {$i + $a + "`n" + 
                "================================================="
                "|         " + $time + " - " + $day + " - " + $date+"        |"          
                "================================================="
                #truncated to save space            
    }
}
您所听到的逻辑将为每台计算机处理每个人
$a
$i

从问题编辑更新

我认为您需要检查每台计算机及其相应的用户,对吗?如果是这种情况,您可以通过单个索引循环执行。有更好的方法来执行此操作,但这将与您当前的代码很好地融合

For($index=0;$index -lt $people.Count; $index++){
    $a = $people[$index]
    $i = $computers[$index]

    $i + $a + "`n" +
    #... Process Stuff Here

}

用它来代替当前代码中的两个for循环。

我还没有检查所有代码,但有一个问题我看到这行是否正确

foreach ($a in $people){}
虽然这会起作用,但分配给它的表达式是空的
{}
。您需要将右大括号移动到脚本的末尾

foreach ($a in $people){}    
foreach ($i in $computers)

    {$i + $a + "`n" + 
                "================================================="
                "|         " + $time + " - " + $day + " - " + $date+"        |"          
                "================================================="
                #truncated to save space            
    }
应该是

foreach ($a in $people){    
foreach ($i in $computers)

    {$i + $a + "`n" + 
                "================================================="
                "|         " + $time + " - " + $day + " - " + $date+"        |"          
                "================================================="
                #truncated to save space            
    }
}
您所听到的逻辑将为每台计算机处理每个人
$a
$i

从问题编辑更新

我认为您需要检查每台计算机及其相应的用户,对吗?如果是这种情况,您可以通过单个索引循环执行。有更好的方法来执行此操作,但这将与您当前的代码很好地融合

For($index=0;$index -lt $people.Count; $index++){
    $a = $people[$index]
    $i = $computers[$index]

    $i + $a + "`n" +
    #... Process Stuff Here

}

用它来代替当前代码中的两个for循环。

我还没有检查所有代码,但有一个问题我看到这行是否正确

foreach ($a in $people){}
虽然这会起作用,但分配给它的表达式是空的
{}
。您需要将右大括号移动到脚本的末尾

foreach ($a in $people){}    
foreach ($i in $computers)

    {$i + $a + "`n" + 
                "================================================="
                "|         " + $time + " - " + $day + " - " + $date+"        |"          
                "================================================="
                #truncated to save space            
    }
应该是

foreach ($a in $people){    
foreach ($i in $computers)

    {$i + $a + "`n" + 
                "================================================="
                "|         " + $time + " - " + $day + " - " + $date+"        |"          
                "================================================="
                #truncated to save space            
    }
}
您所听到的逻辑将为每台计算机处理每个人
$a
$i

从问题编辑更新

我认为您需要检查每台计算机及其相应的用户,对吗?如果是这种情况,您可以通过单个索引循环执行。有更好的方法来执行此操作,但这将与您当前的代码很好地融合

For($index=0;$index -lt $people.Count; $index++){
    $a = $people[$index]
    $i = $computers[$index]

    $i + $a + "`n" +
    #... Process Stuff Here

}

用它代替当前代码中的两个for循环。

当脚本结束时,它会更改用户名(检查所有计算机)代码正在按我的指示运行,现在从您的更新中,我想我看到了您想要的。是否要合并文件的内容?即
$people
第一行中的用户与
$computers
中的第一行匹配。您使它看起来像是要检查所有计算机上的所有用户。两个文本文件的长度是否相同h然后呢?是的,我想合并。作为你的替补。是的,他们是相同的长度。我再次阅读了帖子,看到用用户名连接计算机名。问题的标题是误导性的,因为错误的
For
循环。我希望这次我做对了。那么,我是否要删除foreach($a in$people){foreach($I in$computers)并添加您的For($index…)在脚本结束时更改用户名(检查所有计算机)代码正在按我的指示运行,现在从您的更新中,我想我看到了您想要的。是否要合并文件的内容?即
$people
第一行中的用户与
$computers
中的第一行匹配。您使它看起来像是要检查所有计算机上的所有用户。两个文本文件的长度是否相同h然后呢?是的,我想合并。作为你的替补。是的,他们是相同的长度。我再次阅读了帖子,看到用用户名连接计算机名。问题的标题是误导性的,因为错误的
For
循环。我希望这次我做对了。那么,我是否要删除foreach($a in$people){foreach($I in$computers)并添加您的For($index…)在脚本结束时更改用户名(检查所有计算机)代码正在按我的指示运行,现在从您的更新中,我想我看到了您想要的。是否要合并文件的内容?即
$people
第一行中的用户与
$computers
中的第一行匹配。您使它看起来像是要检查所有计算机上的所有用户。两个文本文件的长度是否相同然后呢?是的我想合并当你的替补