简体   繁体   English

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

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

Using powershell I want to scan a folder containing several txt-files (backup logfiles) and in case of an error create an email with the specific txt-files containing the error messages attached to the mail. 使用powershell我想扫描包含多个txt文件(备份日志文件)的文件夹,如果发生错误,请创建一个包含特定txt文件的电子邮件,其中包含附加到邮件的错误消息。

at the moment I identify the logfiles and write their path to a seperate txt-file. 目前,我确定了日志文件并将其路径写入单独的txt文件。

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

I get a txt file containing this (no headers, no blank lines): 我得到一个包含此文件的txt文件(没有标题,没有空白行):

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

Then I want to create an e-mail and attach the files from that seperate txt-file. 然后我想创建一个电子邮件并附加该单独的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)
  }

This is the result I keep getting: 这是我不断得到的结果:

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

In english the error means: "It is not possible to call a method for an expression that has the NULL." 在英语中,错误意味着:“不可能为具有NULL的表达式调用方法。”

The shown paths are correct, the files are there and they have content. 显示的路径是正确的,文件在那里,他们有内容。

Thanks for your help! 谢谢你的帮助!

Your file paths appear to have spaces in them. 您的文件路径似乎包含空格。 What happens if you put "$log" in quotes when you define $attachment ? 如果在定义$attachment时将"$log"放在引号中会发生什么?

Better solution: to avoid all that fussing around with parsing strings out of text files, don't output your file list to text. 更好的解决方案:为了避免因解析文本文件中的字符串而烦恼,不要将文件列表输出到文本。 Just use the file system objects. 只需使用文件系统对象。

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

(Not tested, so check the command returns the files you're looking for) (未经测试,请检查命令返回您要查找的文件)

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

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