使用PowerCLI / PowerShell for gmailembeddedhtml电子邮件时遇到问题

我是Powershell的新手,我是VMware环境的初级pipe理员。 我正在尝试在PowerCli中自动创build快照周报。 我正在Powershell中创build脚本

我已经做了一个简单的CSS,我有电子邮件发送成功与HTML表作为附件。 我用cssuri选项使用convertto-html,然后发送mailmessage。

但是,更好的办法是直接在我们的Gmail收件箱中显示,而不必下载任何附件。 这就是我卡住的地方。 我把它发送到电子邮件,但它显示为纯文本 – 没有很好的格式发生。

用最新的脚本,看起来像这样。

make the "creds" file first and drop it into a directory. Write-Host "`n Starting script, connecting to server" -ForegroundColor Green $creds = Get-VICredentialStoreItem -file "D:\Documents and Settings\creds" connect-viserver -server $creds.Host -User $creds.User -Password $creds.Password Start-Sleep -s 30 Write-Host "`n Connection to server complete" -ForegroundColor Green Write-Host "`n Pulling data from Win03vc02" -ForegroundColor Green $date = Get-Date $dateforname = Get-Date -UFormat '%m-%d-%Y-%H%M%S' $filename = "VMsnapshots_" + $dateforname + ".html" $Report=@" <style type='text/css'> table { Margin: 2px 2px 2px 2px; Border-collapse: collapse; Font-Family: Calibri; Font-Size: 12pt; Background-Color: rgb(252, 252, 252); } table, td { border-style: solid; border-color: #E0E0E0; border-width: 1px; } tr:hover td { Background-Color: #6699CC; Color: rgb(255, 255, 255); } tr:nth-child(even) { Background-Color: #eef; } th { Text-Align: Left; Color: #6699CC; Padding: 5px 5px 5px 5px; Background-Color: #484848; Font-Size: 14pt; } td { Vertical-Align: Top; Padding: 5px 5px 5px 5px; } </style> "@ $Report += Get-VM | Get-Snapshot | Select-Object VM,VMId,Description,PowerState,SizeGB | ConvertTo-Html -Fragment Write-Host "`n Data pulled, sending mail in 60 seconds..." -ForegroundColor green Start-Sleep -s 60 $from = "o" $to = "i","j" $subject = "VMWare snapshots report for week of $date" $smtp = "h" Send-MailMessage -from $from -to $to -subject $subject -Body $Report -BodyAsHtml -smtpServer $smtp Write-Host "`n e-mail will arrive shortly, process complete!" 

成功将其添加为具有正确格式的附件的外观如下所示。 但我不喜欢这种方式。

 # make the "creds" file first and drop it into a directory. Write-Host "`n Starting script, connecting to server" -ForegroundColor Green $creds = Get-VICredentialStoreItem -file "D:\creds" connect-viserver -server $creds.Host -User $creds.User -Password $creds.Password Start-Sleep -s 30 Write-Host "`n Connection to server complete" -ForegroundColor Green Write-Host "`n Pulling data from Win03vc02" -ForegroundColor Green $date = Get-Date $dateforname = Get-Date -UFormat '%m-%d-%Y-%H%M%S' $filename = "VMsnapshots_" + $dateforname + ".html" $Attachment = "D:\$filename" $css = "D:\htmlstyle2.css" $Report = Get-VM | Get-Snapshot | Select-Object VM,VMId,Description,PowerState,SizeGB | ConvertTo-Html -CssUri $css | Out-File -Encoding ascii "$Attachment" Write-Host "`n Data pulled, sending mail in 60 seconds..." -ForegroundColor green Start-Sleep -s 30 $from = "t" $to = "a" $subject = "VMWare snapshots report for week of $date" $smtp = "a" Send-MailMessage -from $from -to $to -subject $subject -body "Hello, attached is the snapshot report for this week on $date. `nPlease download it first, then open it. ` Timestamp follows the format month-day-year_hours-minutes-seconds" -Attachments "$Attachment" -smtpServer $smtp Write-Host "`n e-mail will arrive shortly, process complete!" 

Gmail删除样式标签。 你将需要把你的风格内联。 在$ Report上执行string操作可能是最简单的。 例如:

 $newhtml = $Report -replace ('<td','<td style="color:red;"')