Requesting to assist with to have this backup report output to be sent as an Email or with as an attachment to an email
hello,
i have below script working when executed database backup details, script intends file gets stored in disk output file i.e html document.
i need assistance in getting file emailed me subject line , have smtp server working , can assist me needs appended below script
-------------------------------------------------------
#change value of following variables needed
$serverlist = get-content "d:\backupreport\serverlist.txt"
$outputfile = "d:\backupreport\output.htm"
$html = '<style type="text/css">
#header{font-family:"trebuchet ms", arial, helvetica, sans-serif;width:100%;border-collapse:collapse;}
#header td, #header th {font-size:14px;border:1px solid #98bf21;padding:3px 7px 2px 7px;}
#header th {font-size:14px;text-align:left;padding-padding-bottom:4px;background-color:#a7c942;color:#fff;}
#header tr.alt td {color:#000;background-color:#eaf2d3;}
</style>'
$html += "<html><body><table border=1 cellpadding=0 cellspacing=0 width=100% id=header>
<tr>
<th><b>database name</b></th>
<th><b>recoverymodel</b></td>
<th><b>last full backup date</b></th>
<th><b>last differential backup date</b></th>
<th><b>last log backup date</b></th>
</tr>"
[system.reflection.assembly]::loadwithpartialname('microsoft.sqlserver.smo') | out-null
foreach ($servername in $serverlist)
{
$html += "<tr bgcolor='#ccff66'><td colspan=5 align=center>$servername</td></tr>"
$sqlserver = new-object ('microsoft.sqlserver.management.smo.server') $servername
foreach($database in $sqlserver.databases)
{
$html += "<tr>
<td>$($database.name)</td>
<td>$($database.recoverymodel)</td>
<td>$($database.lastbackupdate)</td>
<td>$($database.lastdifferentialbackupdate)</td>
<td>$($database.lastlogbackupdate)</td>
</tr>"
}
}
$html += "</table></body></html>"
$html | out-file $outputfile
-----------------------------------------
regards
eben
do want report email body or attachment? way want go, belowshould work, append bottom of script
this should add html directly body
send-mailmessage -to "user01 <user01@example.com>" -from "user02 <user02@example.com>" -subject "report" -body $html -bodyashtml -smtpserver <your smtp server>
this should add file attachment
send-mailmessage -to "user01 <user01@example.com>" -from "user02 <user02@example.com>" -subject "report" -body "report has been attached" -attachments "d:\backupreport\output.htm" -smtpserver <your smtp server>
if find post has answered question, please mark answer. if find post helpful in anyway, please click vote helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''
Windows Server > Windows PowerShell
Comments
Post a Comment