Powershell-无法发送邮件HTML消息 [英] Powershell - Unable to send mail HTML message

查看:61
本文介绍了Powershell-无法发送邮件HTML消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Powershell脚本,该脚本将创建新部署的VM的HTML报告并将其作为电子邮件发送.到目前为止,我已经尝试了很多东西.但是没有运气.不幸的是,我无法收到邮件.我在哪里错了?这是脚本的相关部分...

I'm using a powershell script that will create an HTML report of new deployed VM and send it as an email. I have tried a lot of stuff so far. But no luck. Unfortunately , I am unable to get mail. Where am I wrong ? Here are the relevant parts of the script...

$Date = get-date
$Datefile = ( get-date ).ToString(‘yyyy-MM-dd-hhmmss’)
$ErrorActionPreference = "SilentlyContinue"
# Variable to change
$HTML = "yes"


#Add Text to the HTML file
Function Create-HTMLTable
{
param([array]$Array)
$arrHTML = $Array | ConvertTo-Html
$arrHTML[-1] = $arrHTML[-1].ToString().Replace(‘</body></html>’,"")
Return $arrHTML[5..2000]
}

$Header = "
<html><head></head><body>
<style>table{border-style:solid;border-width:1px;font-size:8pt;background-color:#ccc;width:100%;}th{text-align:left;}td{background-color:#fff;width:20%;border-style:so
lid;border-width:1px;}body{font-family:verdana;font-size:12pt;}h1{font-size:12pt;}h2{font-size:10pt;}</style>
<H1>VMware VM information</H1>
<H2>Date and time</H2>,$date
"


$Report = @()
Get-VM $row.ServerName | %

 {

  $vm = Get-View $_.ID
    $vms = "" | Select-Object VMName, Hostname, IPAddress
    $vms.VMName = $vm.Name
    $vms.Hostname = $vm.guest.hostname
$vms.IPAddress = $vm.guest.ipAddress

$Report += $vms
}

if ($HTML -eq "yes") {
$output += ‘<p>’
$output += ‘<H2>VMware VM information</H2>’
$output += ‘<p>’
$output += Create-HTMLTable $reports
$output += ‘</p>’
$output += ‘</body></html>’ }


Send-MailMessage -to $emailto -Subject $subject -SmtpServer $smtp -From $fromaddress -Body ($output) -BodyAsHtml

最新更新:

但是,当我每次运行脚本时,都会收到重复的邮件,如下所示.听起来这些值会附加到变量中.

But When I run each time script , I am getting duplicate mail like below. it sounds like these values appends into the variable.

邮件正文:

VMware VM information
Date and time
05/14/2020 17:24:51 
VMware VM information
VMName, Hostname, IPAddress
VM01,  Vm01 , xx.xx.xx.xx
VM02,  Vm02 , xx.xx.xx.xx
VMware VM information
VMName, Hostname, IPAddress
VM01,  Vm01 , xx.xx.xx.xx
VM02,  Vm02 , xx.xx.xx.xx

推荐答案

有些事情我觉得不对.

使用参数 $ reports 调用辅助函数 Create-HTMLTable ,但这是一个错字,因为该变量实际上称为 $ Report .
此外,该函数使用 ConvertTo-Html 从数组中创建html,而无需使用 -Fragment 开关,然后尝试删除放入的多余html.
使用 -Fragment 开关时,将不需要这样做.

The helper function Create-HTMLTable is called with parameter $reports, but that is a typo, because the variable is actually called $Report.
Also, the function creates html from an array using ConvertTo-Html without the -Fragment switch and then tries to remove the extra html that is put in.
When using the -Fragment switch, there would be no need for that.

接下来,在构建$ Report时,您正在使用 $ row.ServerName ,但这似乎从未定义.

Next, when building the $Report, you are using $row.ServerName, but that seems never defined.

尝试:

$Date     = Get-Date
$Datefile = '{0:yyyy-MM-dd-hhmmss}' -f $Date    # not sure why you need this
$ErrorActionPreference = "SilentlyContinue"

# Variable to change. Make this a Boolean, so it can be used directly for the `BodyAsHTML` switch
$HTML = $true

# create Here-String templates for the HTML and for a plain-text output
$htmlBegin = @"
<html><head></head><body>
<style>
    table{border-style:solid;border-width:1px;font-size:8pt;background-color:#ccc;width:100%;}
    th{text-align:left;}td{background-color:#fff;width:20%;border-style:solid;border-width:1px;}
    body{font-family:verdana;font-size:12pt;}h1{font-size:12pt;}h2{font-size:10pt;}
</style>
<H1>VMware VM information</H1>
"@

# the placeholders '{0}' and '{1}' will be filled in later
$htmlEnd = @"
<H2>Date and time: {0}</H2>
<p></p>
<p>{1}</p>
</body></html>
"@

$plainText = @"
VMware VM information

Date and time: {0}

{1}
"@


# get the report for the VMs
$Report = Get-VM | ForEach-Object {
    Get-View $_.ID | Select-Object @{Name = 'VMName'; Expression = { $_.Name }},
                                   @{Name = 'Hostname'; Expression = { $_.guest.hostname }},
                                   @{Name = 'IPAddress'; Expression = { $_.guest.ipAddress }}
}

if ($HTML) {
    # convert the report into a HTML table. Use -Fragment to
    # just the HTML for the table; no '</body></html>'
    $table  = ($Report | ConvertTo-Html -Fragment) -join [Environment]::NewLine
    $output = $htmlBegin + ($htmlEnd -f $date, $table)
}
else {
    $table  = $Report | Format-Table -AutoSize | Out-String
    $output = $plainText -f $date, $table
}

# create a Hashtable for splatting the parameters to Send-MailMessage
$mailParams = @{
    To         = $emailto
    From       = $fromaddress
    Subject    = $subject
    SmtpServer = $smtp
    Body       = $output
    BodyAsHtml = $HTML  # $true of $false
}

Send-MailMessage @mailParams

HTML输出应如下所示:

The HTML output should look something like this:

创建两个HTML模板的原因是我们希望能够使用占位符'{0}'和'{1}',并在以后使用 -f 格式替换它们操作员.因为在第一部分中有样式定义,也使用了 {} 字符,所以如果我们仅在一个模板中执行此操作,则所有这些现有花括号都需要翻倍,否则 -f 将无法找到和替换占位符.

The reason for creating two HTML templates is that we want to be able to use placeholders '{0}' and '{1}' and replace these later using the -f Format operator. Because in the first part there are style definitions, also using { and } characters, if we do this in just one template, all of these existing curly braces would need to be doubled, otherwise -f will not be able to find and replace the placeholders.

这篇关于Powershell-无法发送邮件HTML消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆