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

Welcome Forums General PowerShell Q&A Report is HTML File would like to place that into the Body of The Email

This topic contains 8 replies, has 4 voices, and was last updated by

 
Participant
2 weeks, 5 days ago.

  • Author
    Posts
  • #126644

    Participant
    Points: 117
    Rank: Participant

    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" 
    
  • #126645

    Participant
    Points: 263
    Helping Hand
    Rank: Contributor

    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!!

  • #126648

    Participant
    Points: 324
    Helping Hand
    Rank: Contributor
  • #126656

    Participant
    Points: 117
    Rank: Participant

    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

     

     

  • #126657

    Participant
    Points: 117
    Rank: Participant

    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?

  • #126659

    Participant
    Points: 110
    Helping Hand
    Rank: Participant

    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?

  • #126663

    Participant
    Points: 117
    Rank: Participant

    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: (:) [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

    Thoughts

  • #126798

    Participant
    Points: 117
    Rank: Participant

    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

     

     

  • #126807

    Participant
    Points: 110
    Helping Hand
    Rank: Participant

    Guys,

    I got it working

    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!

You must be logged in to reply to this topic.