1

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?

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

is it possible to put a group of scripts in a { ... } and tee the output to file?
I would doubt it. I suspect that if you do anything to redirect the output of the script to a file, that file will be locked as long as the file is running.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.