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?
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.