使用Remove-Item后,无法在powershell上执行附件脚本

时间:2015-10-06 02:59:16

标签: powershell office365 powershell-v3.0

我正在尝试自动化一个脚本,我将向自己和另一个管理员发送输出。脚本的开始检查文件是否存在它是否删除,以便它可以创建另一个输出由脚本附加相同的文件名但我得到一个错误

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

脚本如下: -

$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

有人可以帮我解决这里出了什么问题。我知道有一件事这个文件没有被修改,因为它正在使用,但我注意到为什么它正在使用。

提前致谢

0 个答案:

没有答案