When migrating Html content from a CMS database or other sources you might find the Html as an Html encoded string.
Example:
<p><strong>Some Text</strong></p>
But you want to have the string look like this:
<p><strong>Some Text</strong></p>
The following script is a simple PowerShell script to convert an exisiting file containing the Html encoded text and save the decoded string to a new output file.
param( [string]$InputFile, [string]$OutputFile ) Add-Type -AssemblyName System.Web Write-Output "Fetching $($InputFile)" $fileContent = Get-Content $InputFile Write-Output "Converting" [System.Web.HttpUtility]::HtmlDecode($fileContent) | Out-File -FilePath $OutputFile -Encoding utf8 -Force
.\Convert-ToHtml.ps1 -InputFile '.\InputFile.txt' -OutputFile '.\Output.html'
Enjoy!
The PowerShell script Get-Diskspace.ps1 uses a pretty basic inline CSS approach to generate a more nicely html email from a PowerShell output.
You can use the following description for your own PowerShell script.
First a new html block is configured and the inline CSS is embedded into the head tag.
# Some CSS to get a pretty report # Variable containing the inline CSS and the full html head tag $head = @' <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd"> <html><head><title>$($ReportTitle)</title> <style type=”text/css”> <!– body { font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; } h2{ clear: both; font-size: 100%;color:#354B5E; } h3{ clear: both; font-size: 75%; margin-left: 20px; margin-top: 30px; color:#475F77; } table{ border-collapse: collapse; border: none; font: 10pt Verdana, Geneva, Arial, Helvetica, sans-serif; color: black; margin-bottom: 10px; } table td{ font-size: 12px; padding-left: 0px; padding-right: 20px; text-align: left; } table th { font-size: 12px; font-weight: bold; padding-left: 0px; padding-right: 20px; text-align: left; } -> </style> '@
The recurring data content is added to a global varibale using the -Fragment attribute. This ensures that no full html document data is being created.
$global:Html += $wmi | ConvertTo-Html -Fragment -PreContent "<h2>Server $($ServerName)</h2>"
Before sending the html result the full body html is generated by combinding the html fragments and the manually defined html head.
[string]$Body = ConvertTo-Html -Body $global:Html -Title "Status" -Head $head Send-Mail -From $MailFrom -To $MailTo -SmtpServer $MailServer -MessageBody $Body -Subject $ReportTitle
Enjoy.