title: Adding multiple attachments to email with paths from txt file via powershell
body: <p>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.</p>
  
  <p>at the moment I identify the logfiles and write their path to a seperate txt-file.</p>
  
  <pre><code>Get-ChildItem $pfad -Filter *Error*.txt -Recurse | Select-String "Error occured" | Select-Object -expandproperty Path | Out-File -FilePath $filelist
  </code></pre>
  
  <p>I get a txt file containing this (no headers, no blank lines):</p>
  
  <pre><code>D:\Share\test2\Neues TextdokumentError.txt
  D:\Share\test2\Testfile fbibgfwErroriebgfvibfvsbvf full.txt
  </code></pre>
  
  <p>Then I want to create an e-mail and attach the files from that seperate txt-file.</p>
  
  <pre><code>$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)
    }
  </code></pre>
  
  <p>This is the result I keep getting:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>In english the error means: "It is not possible to call a method for an expression that has the NULL."</p>
  
  <p>The shown paths are correct, the files are there and they have content.</p>
  
  <p>Thanks for your help!</p>