我正在使用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