1

The below script puts database backup details into a nicely formatted table with borders and saves as a .htm. It then emails the report, but when the report is emailed the 'table' has no borders and no gap between the 'LastBackupDate' column and the 'LastLogBackupDate' column - it basically looks the same as the results would on the powershell console. Can anyone tell me how to format the email so all the html is used from the file? P.s. I can't use send-mailmessage due to issues with gmail ssl. Thanks.

#HTML 
$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>"

#Set Date
$date = ( get-date ).ToString('yyyyMMdd')

#Locate DB
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | out-null
$s = New-Object ('Microsoft.SqlServer.Management.Smo.Server') "LOCALHOST\SQLX64"

#Retrieves the last backup dates - for full and log backups
$dbs=$s.Databases
$backups = $dbs | SELECT Name,LastBackupDate, LastLogBackupDate | Sort-Object LastBackupDate | ConvertTo-HTML -head $a -body "<H2>DB01 Database Backup Details $date </H2>" | Out-File $("D:\SQL_Backup_Log_Script\Logs\Backup_Log_Temp.htm")

#Email Report
$EmailFrom = "[email protected]"
$emailto = "[email protected]"
$Subject = "Database Backup Log $date" 
$Body = Get-Content D:\SQL_Backup_Log_Script\Logs\Backup_Log_Temp.htm
$SMTPServer = "smtp.gmail.com" 
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587) 
$SMTPClient.EnableSsl = $true 
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("[email protected]", "password here"); 
$message = New-Object Net.Mail.MailMessage($EmailFrom, $EmailTo, $Subject, $Body)
$message.IsBodyHtml = $true;
$SMTPClient.Send($message)

#Rename file to include today's date
Rename-Item -path D:\SQL_Backup_Log_Script\Logs\Backup_Log_Temp.htm -newname ($date +"_DB01_Backup_Log.htm")
5
  • 2
    As an aside, consider replacing your "$a = $a + " repetition with 'here strings' in PowerShell. It'll make your life a little easier. technet.microsoft.com/en-us/library/ee692792.aspx Commented Feb 8, 2012 at 14:53
  • +1 @xcud. Charlotte, the file looks OK in your browser before you send it out? Commented Feb 8, 2012 at 15:34
  • Shay, yes the .htm file is looking good. Commented Feb 8, 2012 at 16:16
  • @xcud - thanks, yes that is useful and will make life easier. Commented Feb 8, 2012 at 16:17
  • Adapted to my databes it works perfectly. Commented Feb 9, 2012 at 7:10

1 Answer 1

1

Try change this line (can't test if is this the problem):

$Body = [System.IO.File]::ReadAllText("D:\SQL_Backup_Log_Script\Logs\Backup_Log_Temp.htm")

the body property in Net.Mail.MailMessage accept [string], get-content return [string[]].

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks but the emailed report hasn't changed.
Just realised, the email has been formatting fine the whole time, just GMAIL won't display the formatting. It looks good in Outlook!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.