0

I am using a standard SendMessage command to send an email in a PowerShell script. I want to attach a log file but unfortunatly the log file is locked by a parent script.

Is there a way to send the file anyways as an attachment (I don't want to delete it or write it)?

I know I can read the file with Get-Content, should I pipe this output to a new temporary file that is then attached? If that would work how would I go about doing this.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
l.wach
  • 67
  • 1
  • 1
  • 10
  • 2
    Locked file is locked. You could find and forcefully close the handle the parent has on that file, but I wouldn't recommend that, as it may break things for the parent process. If you can read the file via `Get-Content` your mail client should be able to read the file as well. If you have control over the parent, try to make it not keep the file open all the time. Otherwise I'd say you're out of luck. – Ansgar Wiechers Jun 08 '17 at 09:23
  • 1
    If the "standard SendMessage command" you are using is `Send-MailMessage` then please update your question, else people might wonder if you are using something different (and that could matter very much). – Charlie Joynt Jun 08 '17 at 09:42
  • 1
    And are you really using powershell-v1.0 (as per the tag)? Check the version of PowerShell with `$host`. Also on the topic of tags, the *sendmessage* tag refers to a non-email function. – Charlie Joynt Jun 08 '17 at 09:44
  • 1
    Since he claims to be using PowerShell v1 (I removed the mention in the text and added the appropriate tag) he doesn't have `Send-MailMessage`. AFAIK the cmdlet was introduced with PowerShell v2. – Ansgar Wiechers Jun 08 '17 at 11:46
  • The question was not about how to send a mail message, but how to attach a locked file. – Adam Mnich Jun 08 '17 at 11:52

1 Answers1

2

Copy-Item should work if you can read the content with Get-Content.

Copy it as a temporary file, send it, then delete it.

Copy-Item C:\My_log.log $env:TEMP

PowerShell 2.0 and above

Send-MailMessage -Attachments $env:TEMP\My_log.log .......

PowerShell 1.0 -> PowerShell Send Email

Remove-Item $env:TEMP\My_log.log
Adam Mnich
  • 461
  • 3
  • 12