Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Email Powershell Send MailMessage在发送失败后不处理附件_Email_Powershell_Dispose - Fatal编程技术网

Email Powershell Send MailMessage在发送失败后不处理附件

Email Powershell Send MailMessage在发送失败后不处理附件,email,powershell,dispose,Email,Powershell,Dispose,我的自动Powershell Hyper-V备份脚本有点问题。该脚本功能完美,但我的问题是,当我试图发送一封带有日志文件作为附件的电子邮件时,它在发送失败后不会解锁该文件。(当它成功发送邮件时,我无法检查它的行为,但老实说,它也应该处理它,即使它无法发送。) 我在网上搜索过,我能找到的是: 发送邮件消息发送后自动处理附件 邮件 将Powershell v1中的方法用于新对象 system.net.mail.mailmessage和新对象 system.net.mail.attachment 以下

我的自动Powershell Hyper-V备份脚本有点问题。该脚本功能完美,但我的问题是,当我试图发送一封带有日志文件作为附件的电子邮件时,它在发送失败后不会解锁该文件。(当它成功发送邮件时,我无法检查它的行为,但老实说,它也应该处理它,即使它无法发送。)

我在网上搜索过,我能找到的是:

  • 发送邮件消息发送后自动处理附件 邮件
  • 将Powershell v1中的方法用于新对象 system.net.mail.mailmessage和新对象 system.net.mail.attachment
  • 以下是我的代码部分(我已将其缩短为所需代码的要点):

    所以我的问题是,在发送电子邮件失败后,如何使Send MailMessage处理附件,以便我的脚本可以继续写入日志文件

    当邮件成功发送时,上面的脚本可以正常工作,不会抛出任何错误,也不会锁定文件

    亲切问候,

    托马斯·施密特

    编辑:

    我选择了旧的Powershell v1方式:

    Try
    {
        $SMTPServer = "exchange.example.dk"
        $SMTP = new-object Net.Mail.SmtpClient($SMTPServer)
    
        $EmailAttachment = new-object Net.Mail.Attachment($Logfile)
        $EmailMessage = new-object Net.Mail.MailMessage
    
        $EmailMessage.Attachments.Add($EmailAttachment)
        $EmailMessage.From = "service@example.dk"
        $EmailMessage.To.Add("spport@example.dk")
        $EmailMessage.Subject = "Notification from email server"
        $EmailMessage.Body = "Hello,
    An error occurred during A/S Hyper-V Backup on $ComputerName.
    
    I have attached the log file, $LogFile, to this mail.
    
    Kind Regards,
    Hyper-V Backup.
    "
        $SMTP.Send($EmailMessage)
        $EmailAttachment.Dispose()
        Write-Host "# $(Get-Date) :: Successfully send log file to 'Support@example.dk" -foregroundcolor green; "# $(Get-Date) :: Successfully send log file to 'Support@example.dk" | Out-File $LogFile -Append
    }
    Catch
    {
        $EmailAttachment.Dispose()
        Write-Host "# $(Get-Date) :: Failed to send log file to 'Support@example.dk" -foregroundcolor red; "# $(Get-Date) :: Failed to send log file to 'Support@example.dk" | Out-File $LogFile -Append
        "                      :: Error Command: SMTP.Send(EmailMessage)" | Out-File $LogFile -Append
        "                      :: Error Message: $($_.Exception.Message)" | Out-File $LogFile -Append
    }
    
    但是,如果有人能提供上述问题的答案,我愿意使用该解决方案

    亲切问候,


    托马斯·施密特(Thomas Schmidt)

    这就是我处理这件事的方式,它似乎对我有用:

     try {
        $ErrorActionPreference = 'Stop'
        $SmtpSettings = @{
            To = $Recipients.Split(',')
            From = $Sender
            SmtpServer = $SmtpServer
            Subject = "Email test"
            Body = "Please check attached file"
            Attachment = $Attachment
        }
        Send-MailMessage @SmtpSettings -BodyAsHtml
     } catch {
        $ErrorActionPreference = 'Continue'
        [gc]::collect()
        $CustomError = ($_ -replace "`r`n","")
        $ScriptLine = "$($_.InvocationInfo.ScriptLineNumber)" +','+ "$($_.InvocationInfo.OffsetInLine)"
        Write-Verbose "Error sending email: $CustomError (script line $ScriptLine)"
    }
    
    我基本上使用了垃圾收集器-[gc]::collect(),尽管在发送电子邮件时出错,它还是解锁了附件文件。
    我还将$ErrorActionPreference='Stop'设置在try中,这样它就可以正确地捕捉错误,而$ErrorActionPreference='Continue'设置在catch中,这样脚本就不会完全停止。

    试试这种旧样式。对我来说很好

    #Send Email
    $smtpServer = "10.10.10.10"
    $file = "C:\Temp\TEST.zip"
    $att = new-object Net.Mail.Attachment($file)
    $msg = new-object Net.Mail.MailMessage
    $smtp = new-object Net.Mail.SmtpClient($smtpServer)
    $msg.From = "MYNAME@DOMAIN.com"
    $msg.To.Add("yourname@domian.com")
    $msg.CC.Add("yourname2@domain.com")
    $msg.Subject = "Subject here"
    $msg.Body = "Body here"
    $msg.Attachments.Add($att)
    $smtp.Send($msg)
    $att.Dispose()
    
     try {
        $ErrorActionPreference = 'Stop'
        $SmtpSettings = @{
            To = $Recipients.Split(',')
            From = $Sender
            SmtpServer = $SmtpServer
            Subject = "Email test"
            Body = "Please check attached file"
            Attachment = $Attachment
        }
        Send-MailMessage @SmtpSettings -BodyAsHtml
     } catch {
        $ErrorActionPreference = 'Continue'
        [gc]::collect()
        $CustomError = ($_ -replace "`r`n","")
        $ScriptLine = "$($_.InvocationInfo.ScriptLineNumber)" +','+ "$($_.InvocationInfo.OffsetInLine)"
        Write-Verbose "Error sending email: $CustomError (script line $ScriptLine)"
    }
    
    #Send Email
    $smtpServer = "10.10.10.10"
    $file = "C:\Temp\TEST.zip"
    $att = new-object Net.Mail.Attachment($file)
    $msg = new-object Net.Mail.MailMessage
    $smtp = new-object Net.Mail.SmtpClient($smtpServer)
    $msg.From = "MYNAME@DOMAIN.com"
    $msg.To.Add("yourname@domian.com")
    $msg.CC.Add("yourname2@domain.com")
    $msg.Subject = "Subject here"
    $msg.Body = "Body here"
    $msg.Attachments.Add($att)
    $smtp.Send($msg)
    $att.Dispose()