0

I am trying to automate a script where i will be sending output to myself and another admin. The start of the script Checks file if exist it delets so that it can create another output to be attached by the script with the same file name but i get an error

Send-MailMessage : The process cannot access the file 'C:\Users\Administrator\Documents\Transcript.txt' because it is being used by another process.
At C:\Website\My Script\Sendmailformailbox.ps1:12 char:1
+ Send-MailMessage -To $User1 -Cc $User2 -Attachments $Location,$Transcript -SmtpS ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Send-MailMessage], IOException
    + FullyQualifiedErrorId : System.IO.IOException,Microsoft.PowerShell.Commands.SendMailMessage

The Script is as below:-

$FileName = (join-path ([environment]::GetFolderPath('MyDocuments')) "Transcript.txt")
if (Test-Path $FileName) {
  Remove-Item $FileName
}
start-transcript -path (join-path ([environment]::GetFolderPath('MyDocuments')) "Transcript.txt") -NoClobber
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.VisualBasic")
$User1 = [Microsoft.VisualBasic.Interaction]::InputBox("Enter the reciepient email address", "TO Destination")
$User2 = [Microsoft.VisualBasic.Interaction]::InputBox("Enter the CC email address", "CC Destination")
$Location = (join-path ([environment]::GetFolderPath('MyDocuments')) "UserOutput.txt")
$Transcript = (join-path ([environment]::GetFolderPath('MyDocuments')) "Transcript.txt")
$UserCredential = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
Import-PSSession $Session -AllowClobber
Get-Mailbox |FT >$Location
Send-MailMessage -To $User1 -Cc $User2 -Attachments $Location,$Transcript -SmtpServer "smtp.office365.com" -Credential $UserCredential -UseSsl "Office 365 Get-Mailbox Output" -Port "587" -Body "This is an automatically generated message.<br>Your Script output was successfully Exported and sent.<br> Please refer to the script execution log for more info if you donot see the output in this mail<br>Designed By<br><b>Wiliam Christopher<br>Blah<br>Blah<br>Blah<br></b>" -From $User1 -BodyAsHtml
Stop-Transcript

Can some one help me with what is going wrong here. I know one thing this file is not getting modified because its in use but i am note sure why its in use.

Thanks In advance

  • Have you tried swapping your last 2 lines of code? – xXhRQ8sD2L7Z Oct 06 '15 at 03:06
  • check out [the canocial answer on this .Net error](https://stackoverflow.com/questions/26741191/ioexception-the-process-cannot-access-the-file-file-path-because-it-is-being) it is in C# but the debugging steps are similar for powershell and will point to your problem – LinkBerest Oct 06 '15 at 03:08
  • just make a copy of your transcript and send this copy – Loïc MICHEL Oct 06 '15 at 05:33
  • The problem is that the transcript still has a lock on the file as the transcript is only stopped after the email has been sent. Either stop the transcript and then send the email or make a copy as the previous comment has said. – Nick Eagle Oct 06 '15 at 08:19
  • Hi all thanks for the help. I forgot to mention one thing. Since this whole process is going to be automated i would like to add some more script so that it removed or robocopy my previous Transcript.txe to other location . I tried Remove-Item on the first line but somehow that dosnt work out. Any help on that ? – William Christopher Oct 07 '15 at 19:08
  • Hi, Thanks to everyone for your suggestions. Thanks Nick for pointing out the silly mistake i made in my script. I have placed the Stop-Transcript before the email script that worked out. – William Christopher Oct 08 '15 at 15:02

0 Answers0