ca9163d9
ca9163d9

Reputation: 29159

How to open the redirected output file of a running script?

I have the following code in script my.ps1.

....
$attach = New-Object System.Net.Mail.Attachment("c:\my.log")
$msg.Attachments.Add( $attach )

And I want to schedule it running as powershell "& { my.ps1 > c:\my.log }".

However, I got the following error.

New-Object : Exception calling ".ctor" with "1" argument(s): "The process cannot access the file 'c:\my.log' because it is being used by another process."

Is anyway to implement it?

Upvotes: 1

Views: 784

Answers (1)

EBGreen
EBGreen

Reputation: 37720

Using redirection is locking the file. Instead you will need to remove the redirection and write to the file directly in the script using cmdlets like Out-File or Add-Content.

Upvotes: 1

Related Questions