Report is HTML File would like to place that into the Body of The Email

I have a small project to convert all my powershell scripts to send the emails with the output in the body of the email was being sent as an attachment we are trying to limit the attachments on the email system I been successful on many but ran into two that I need expert help on I will post the other later.

My script that I am reworking

$LogFolder = "c:\util\logs"
remove-item -path $LogFolder\tgcsnetdfs.* -Force
remove-item -path $LogFolder\tgcsnetrep.* -Force
remove-item -path $LogFolder\tgcsnetdocdfs.* -Force
remove-item -path $LogFolder\tgcsnetnote.* -Force
dfsradmin health new /rgname:tgcsnetrep /refmemname:tgcs012 /ReportName:C:\UTIL\LOGS\TGCSNETREP
dfsradmin health new /rgname:our.network.tgcsnet.com\tgcsnetdfs\homedirectories /refmemname:tgcs012 /ReportName:C:\UTIL\LOGS\TGCSNETDFS
dfsradmin health new /rgname:our.network.tgcsnet.com\tgcsnetdfs\documentation /refmemname:tgcs012 /ReportName:C:\UTIL\LOGS\TGCSNETDOC
dfsradmin health new /rgname:our.network.tgcsnet.com\tgcsnetdfs\technotes /refmemname:tgcs012 /ReportName:C:\UTIL\LOGS\TGCSNETNOTE
$Server = hostname
$Params = @{
 Subject = "DFS Health Report for $Server"
 Body = "$bigbody"
 From = "no-reply@tgcsnet.com"
 To = "systems-alert@tgcsnet.com"
 smtpserver = "InternalRelay.TGCSNET.COM"
}
Send-MailMessage @Params 

This script outputs the reports as .html files and I was using this before as you can see the attachments.

ANY WAY to get the reports in the body? Thank you Tom

$PSEmailServer = "internalrelay.TGCSNET.COM"
Send-MailMessage -From $From -To $To -Subject $Subject -Body $Body -smtpserver $PSEmailServer -attachment "$LogFolder\tgcsnetrep.html", "$LogFolder\tgcsnetdfs.html", "$LogFolder\tgcsnetdoc.html", "$LogFolder\tgcsnetnote.html" 

You really should always read the complete help for cmdlets you’re using … including the examples

Get-Help Send-MailMessage -Full

… pay special attention to the parameter -BodyAsHtml!!

https://docs.microsoft.com/en-us/powershell/module/dfsr/write-dfsrhealthreport?view=win10-ps
https://blogs.technet.microsoft.com/filecab/2015/06/09/a-windows-powershell-cmdlet-to-get-configs-health-and-backlogs/
https://gallery.technet.microsoft.com/scriptcenter/10ca8b47-b0ec-4910-bdd7-52ce2d4bca41
https://www.bleepingcomputer.com/forums/t/591093/dfsr-health-report-and-powershell-script-that-is-actually-useful/

Guys thanks for the fast response

 

Rob

The first link was helpful the others show using attachments that’s not what I am looking to do.

I can convert the dos program dfsradmin to write-dfsrhealthreport

Then I can use standard powershell commands will play around with that

 

Olaf

-BodyASHtml will give that a try .

 

Will post results

Thanks again

Tom

 

 

Ok I tested

Write-DfsrHealthReport -GroupName tgcsnetrep -ReferenceComputerName tgcs012

This created a file in the same directory .html and .xml

I then tried this

$bigbody = Write-DfsrHealthReport -GroupName tgcsnetrep -ReferenceComputerName tgcs012

$bigbody was empty

How can I get the report in the body as html?

If the command generates a file rather than console output wouldn’t we want to look at importing the content of that file? Unless of course we can redirect that output to the console, then we could capture it in a variable. Might I suggest looking into The “Get-Content” and “ConvertTo-HTML” Cmdlets?

Logan

dfsradmin health new /rgname:tgcsnetrep /refmemname:tgcs012 /ReportName:C:\UTIL\LOGS\TGCSNETREP

$bigbody = Get-Content C:\util\logs\TGCSNETREP.html

$Server = hostname

$Params = @{

Subject = “DFS Health Report for $Server

BodyAsHTML = $bigbody

From = no-reply@tgcsnet.com

To = systems-alert@tgcsnet.com

smtpserver = InternalRelay.TGCSNET.COM

}

Send-MailMessage @Params

Send-MailMessage : Cannot convert ‘System.String’ to the type ‘System.Management.Automation.SwitchParameter’ required by parameter ‘BodyAsHtml’.

At line:1 char:18

  • Send-MailMessage @Params

  • 
    
  • CategoryInfo : InvalidArgument: (:slight_smile: [Send-MailMessage], ParameterBindingException

  • FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Commands.SendMailMessage

 

 

I changed BodyASHTML to just Body email went thru but did not format at all

 

Only one line appeared

<meta http-equiv=“X-UA-Compatible” content=“IE=5”>

Thoughts

Guys

I got it working

 

added this

$bigbody = Get-Content C:\util\logs\TGCSNETREP.html

in the $params

added this BodyAsHtml = $true

 

Now the emails come in the body as HTML

 

Thanks for all the help

 

 

Glad to hear that my suggestion at looking into the Get-Content Cmdlet, in conjunction with the previous suggestion of using the -BodyAsHTML switch parameter helped to get you were you needed to be!