我是 Powershell 新手,是 VMware 环境的初级管理员。我正在尝试在 PowerCli 中自动生成快照的每周报告。我正在用 Powershell 编写脚本。
我制作了一个简单的 css,并成功将 html 表作为附件发送电子邮件。我使用了 convertto-html 和 cssuri 选项,然后发送了 send-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!"
答案1
Gmail 删除了样式标签。您需要将样式内联。对 $Report 执行字符串操作可能是最简单的方法。例如:
$newhtml = $Report -replace ('<td','<td style="color:red;"')