【发布时间】: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