繁体   English   中英

使用来自PowerShell的路径从txt文件添加多个附件到电子邮件

[英]Adding multiple attachments to email with paths from txt file via powershell

使用powershell我想扫描包含多个txt文件(备份日志文件)的文件夹,如果发生错误,请创建一个包含特定txt文件的电子邮件,其中包含附加到邮件的错误消息。

目前,我确定了日志文件并将其路径写入单独的txt文件。

Get-ChildItem $pfad -Filter *Error*.txt -Recurse | Select-String "Error occured" | Select-Object -expandproperty Path | Out-File -FilePath $filelist

我得到一个包含此文件的txt文件(没有标题,没有空白行):

D:\Share\test2\Neues TextdokumentError.txt
D:\Share\test2\Testfile fbibgfwErroriebgfvibfvsbvf full.txt

然后我想创建一个电子邮件并附加该单独的txt文件中的文件。

$logfiles = Get-Content $filelist

ForEach($log in $logfiles)
  {
  Write-Host “Attaching File :- ” $log
  $attachment = New-Object System.Net.Mail.Attachment –ArgumentList $log
  $msg.Attachments.Add($attachment)
  }

这是我不断得到的结果:

Attaching File :-  D:\Share\test2\Neues TextdokumentError.txt
Es ist nicht möglich, eine Methode für einen Ausdruck aufzurufen, der den NULL hat.
In C:\test\Unbenannt1.ps1:43 Zeichen:3
+   $msg.Attachments.Add($attachment)
+   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

Attaching File :-  D:\Share\test2\Testfile fbibgfwErroriebgfvibfvsbvf full.txt
Es ist nicht möglich, eine Methode für einen Ausdruck aufzurufen, der den NULL hat.
In C:\test\Unbenannt1.ps1:43 Zeichen:3
+   $msg.Attachments.Add($attachment)
+   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

在英语中,错误意味着:“不可能为具有NULL的表达式调用方法。”

显示的路径是正确的,文件在那里,他们有内容。

谢谢你的帮助!

您的文件路径似乎包含空格。 如果在定义$attachment时将"$log"放在引号中会发生什么?

更好的解决方案:为了避免因解析文本文件中的字符串而烦恼,不要将文件列表输出到文本。 只需使用文件系统对象。

$logfiles = Get-ChildItem $pfad -Filter *Error*.txt -Recurse | Select-String "Error occured" -List | Select Path

(未经测试,请检查命令返回您要查找的文件)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM