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