我必须发送电子邮件并附加超过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 ','
}
}
请告诉我在这种情况下如何添加多个文件作为附件。
我想你已经很接近了。
尝试
$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 = 'you@yourcompany.com'
To = 'someone@yourcompany.com'
Subject = 'PortErrors'
Body = 'Please see the attached files'
SmtpServer = 'smtp.yourcompany.com'
Attachments = $attachments
# more parameters go here
}
# send the email
Send-MailMessage @mailParams
}
附言:如果文件可能很大,如果在Get-Content
行中添加参数-TotalCount
,则可以稍微加快速度,因此当它读取的最大行数超过4行时就会停止:
if (@(Get-Content -Path $NewFile.FullName -TotalCount 5).Count -gt 4) {
...
}