email - Release a file lock that send-mailmessage leaves on error -
i'm using send-mailmessage cmdlet send copy of log file. however, i'd add line log file status of send.
code:
$to = "toaddress@email.com" $subject = "test email attachment" $from = "fromaddress@email.com" $body = "please find log file attached" $smtpserver = "smtp.server.com" $logfile = "testlog.log" add-content -path $logfile -value "trying email log..." try { send-mailmessage -to $to -subject $subject -from $from -body $body -smtpserver $smtpserver -attachments $logfile -erroraction stop add-content -path $logfile -value "successfully emailed log to: $to" } catch [system.exception] { add-content -path $logfile -value "error emailing log to: $to" } {}
error:
ps e:\> . .\testmail.ps1 add-content : process cannot access file 'e:\testlog.log' because being used process. @ e:\testmail.ps1:16 char:14 + add-content <<<< -path $logfile -value "error emailing log to: $to" + categoryinfo : writeerror: (e:\testlog.log:string) [add-content], ioexception + fullyqualifiederrorid : getcontentwriterioerror,microsoft.powershell.commands.addcontentcommand
this works fine if email succeeds, if fails (say smtp server unavailable), , erroraction set "stop", file lock on log file doesn't released. if erroraction not set "stop", log file gets released, catch block doesn't triggered.
i found reference "dispose" method if you're rolling own mail function, i'm not sure how used send-mailmessage cmdlet: http://petermorrissey.blogspot.com.au/2013/01/sending-email-messages-with-powershell.html
is there command should run release lock, or bug, or going wrong way?
email copy of file or create own email function using powershell 1 methods (one example: how send email attachement using powershell v1?)
Comments
Post a Comment