我必須發送電子郵件并附加內容超過 4 行的檔案。
$Final_File_list = Get-ChildItem -Path "E:\files" -Filter "New_*.txt"
foreach($NewFile in $Final_File_list)
{
$Full_File_Path = $NewFile.FullName
$GetLines = (Get-Content -Path $Full_File_Path | Measure-Object -Line).Lines
if($GetLines -gt 4)
{
[array]$rn_all =$Full_File_Path "," #$Full_File_Path -join ','
}
}
請讓我知道在這種情況下如何將多個檔案添加為附件。
uj5u.com熱心網友回復:
我想你已經非常接近了。
嘗試
$Final_File_list = Get-ChildItem -Path "E:\files" -Filter "New_*.txt"
$attachments = foreach($NewFile in $Final_File_list) {
if (@(Get-Content -Path $NewFile.FullName).Count -gt 4) {
# simply output the FullName so it gets collected in variable $attachments
$NewFile.FullName
}
}
if ($attachments) {
# send your email
$mailParams = @{
From = '[email protected]'
To = '[email protected]'
Subject = 'PortErrors'
Body = 'Please see the attached files'
SmtpServer = 'smtp.yourcompany.com'
Attachments = $attachments
# more parameters go here
}
# send the email
Send-MailMessage @mailParams
}
PS。如果檔案可能會很大,你可以,如果你添加引數加快一點-TotalCount的Get-Content線,所以它停止時,它讀取最多4行以上:
if (@(Get-Content -Path $NewFile.FullName -TotalCount 5).Count -gt 4) {
...
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/380566.html
標籤:电源外壳
