我正在使用PowerShell脚本来搜索远程计算机上的某个文件,并将它们保存到xx.txt文件中,并在扫描正常时发送带有附加文件的电子邮件,如果不是这样只是发送错误信息的电子邮件。 (我有70台电脑的域名)同时处理2个列表
现在我要做的是用用户名加入计算机名称。 (我还是很新的用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 = "[email protected]"
"================================================="
"| 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 = "[email protected]" # send email FROM address
$smtpTo = "[email protected]" # 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 = "[email protected]" # send email FROM address
$smtpTo = "[email protected]" # 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中的$ people){XXXXXXXXX}或类似的东西缺失,但我仍然不明白。
如果有人可以帮助,我将不胜感激。
问候
的Neven Gotovac
UPDATE:
试着用下面的答案,但它没有工作,或者我已经把它错了行。 脚本的输出如下所示。如果你检查你会看到那个名字“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 |
=================================================
@马特 我已经把括号像你说的,但它仍然重复的用户名:(是不是一个问题,如果用户名在中间firstname.lastname点在people.txt文件
是否应该有人与计算机之间的关系?就像马特说的,你现在的代码列出了每一台电脑的每个人。 – 2014-10-17 16:20:56
是的,它假设是关系。例如:c01 - augustin – 2014-10-17 16:27:56