I am new to Powershell, I am a junior admin for a VMware environment. I am trying to automate a weekly report for snapshots in PowerCli. I am making the script in Powershell.
I have made a simple css, and I had the e-mail send successfully out with the html table as an attachment. I used convertto-html with the cssuri option, then send-mailmessage after that.
But, even better, would be to have it show up directly in our gmail inbox, without having to download any attachment. That's where I'm stuck. I got it to email, but it shows it as plain text - no nice formatting happens.
With the latest script, it looks like this.
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!"
the one that successfully adds it as an attachment with correct formatting looks like this. but i don't prefer this way.
# 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!"