【问题标题】:Processing 2 lists simultaneously同时处理 2 个列表
【发布时间】:2014-10-17 15:31:06
【问题描述】:

我正在研究 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} 或类似内容中缺少某些内容,但我仍然不明白是什么。

如果有人可以提供帮助,我将不胜感激。

问候

内文·戈托瓦茨

更新:

尝试了下面的答案,但它没有用,或者我把它放在了错误的行。 脚本的输出看起来像这样。如果你检查你会看到名字“augustin”总是重复。相反,它应该像计算机名称一样更改。

  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  |
  =================================================

@马特 正如你所说,我已经放了括号,但它仍然重复用户名:(如果用户名在 people.txt 文件中的中间 firstname.lastname 中有点,这是一个问题

【问题讨论】:

  • 人与电脑之间应该有关系吗?正如马特所说,您当前的代码列出了每台计算机的每个人。
  • 是的,应该是关系。示例:c01 - 奥古斯丁

标签: powershell foreach


【解决方案1】:

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

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

您所听到的逻辑会为每台计算机 $i 处理每个人 $a

问题编辑更新

我认为您需要检查每台计算机及其相应的用户,对吗?如果是这种情况,您可以遍历单个索引循环。有一种更好的方法可以做到这一点,但这会很好地与您当前的代码融合。

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

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

}

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

【讨论】:

  • 脚本结束时更改用户名(检查所有计算机)
  • 代码按照我的要求运行,现在从你的更新中我想我看到了你想要的。是否要合并文件的内容?即$people 第一行的用户匹配$computers 的第一行。您看起来好像要检查所有计算机上的所有用户。那么两个文本文件的长度是否相同?
  • 是的,我想合并。正如你所了解的。是的,它们的长度相同。
  • 我又看了一遍帖子,看到用用户名加入计算机名。问题的标题具有误导性,曲解的For 循环也是如此。我希望这次我做对了。
  • 那么,我是否删除 foreach ($a in $people){ foreach ($i in $computers) 并添加您的 For($index...)
猜你喜欢
  • 1970-01-01
  • 2015-05-18
  • 2012-11-22
  • 1970-01-01
  • 2021-04-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-03
相关资源
最近更新 更多