I am trying to write a powershell script that will be kicked off by a scheduled task, the script will return details of our database backups, save these to a .htm file and also email the results.
So far getting the backup info and saving to a .htm file works fine, but I can’t figure out how to get the body of the email to look like the .htm file. The email body at the moment looks like the raw html code.
So far:
$a = "<style>"
$a = $a + "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}"
$a = $a + "TH{border-width: 1px;padding: 10px;border-style: solid;border-color: black;}"
$a = $a + "TD{border-width: 1px;padding: 10px;border-style: solid;border-color: black;}"
$a = $a + "</style>"
$date = ( get-date ).ToString('yyyyMMdd')
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | out-null
$s = New-Object ('Microsoft.SqlServer.Management.Smo.Server') "LOCALHOST\SQLX64"
$dbs=$s.Databases
#Retrieves the last backup dates - both for FULL and LOG backups
$backups = $dbs | SELECT Name,LastBackupDate, LastLogBackupDate | ConvertTo-HTML -head $a -body "<H2>Database Backup Details $date </H2>" | Out-File $("D:\SQL Backup Log Script\Logs\"+ $date +"_DB01 Backup Log.htm")
$EmailFrom = "IT@##.net"
$emailto = "##@##.net"
$Subject = "DB01 SQL Backup Log"
$Body = Get-Content ("D:\SQL Backup Log Script\Logs\"+ $date +"_Backup Log.htm")
$SMTPServer = "smtp.gmail.com"
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("##", "##");
$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)
Thanks,
Charlotte.
Try add this line before send() method:
EDIT after comment:
You have to use this object:
$SMTPClient = New-Object System.Net.Mail.MailMessage