Reputation: 155
I have an interesting problem. I'm trying to embed HTML tags inside string variables in a PowerShell script, so that as it finds servers in a "down" state, it will display a graphic instead of a number when output to an HTML report page. However, when I open the report, instead of the graphic, I see the HTML code for the graphic. This of course will not do.
My code snippet is as follows:
$working | foreach-object {
{ # Triage is less than 5 days
$graphic = 'greystate\0-5.png'
$triage = '5-15 Days'
}
add-member -InputObject $_ -MemberType NoteProperty -Name TriageInd -Value "`<img href=""$graphic"" alt=""$triage""`>"
}
$working | Select-Object * -exclude Triage | convertTo-HTML -as Table -Title "Grey State Report - $surveydate" -body "<H4>The Following systems are currently not reporting to SCOM as of $surveydate :</H4></P>" -post "</p> This report generated by PowerShell, run on $runfrom by $runby." > $resultsHTML
This is supposed to add an image tag with a filename stored in $graphic and alt text stored in $triage (the code actually goes thru an If..Elseif..Else function to change the specifics, but you get the idea)
However, the output is like this:
OFFLINESERVER.FQDN MG3 4/17/2014 4:56:03 AM TimedOut OFFLINESERVER 0 <img href="greystate\0-5.png" alt="5-15 Days">
The source of the HTML page looks like this:
....
<td>OFFLINESERVER.FQDN</td><td>MG3</td><td>4/17/2014 4:56:03 AM</td><td>TimedOut</td><td>OFFLINESERVER</td><td>0</td><td>&lt;img href="greystate\0-5.png" alt="5-15 Days"&gt;</td><td></td><td></td></tr>
....
Note the section above: <img href="greystate\0-5.png" alt="5-15 Days">
How do I get ConvertTo-HTML to leave my tag punctuation alone so that it will render properly in the browser?
Upvotes: 0
Views: 5283
Reputation: 111
Use these two line in your script to resolve this error
Add-Type -AssemblyName System.Web
[System.Web.HttpUtility]::HtmlDecode($Results ) | Out-File .\Result.html
Upvotes: 3
Reputation: 36322
Well, one solution would be to pipe your HTML to Out-String, then run a RegEx replace on that string to convert ><td>&lt;img href="greystate\0-5.png" alt="5-15 Days"&gt;</td><
back into ><td><img href="greystate\0-5.png" alt="5-15 Days"></td><
$Results = $working | Select-Object * -exclude Triage | convertTo-HTML -as Table -Title "Grey State Report - $surveydate" -body "<H4>The Following systems are currently not reporting to SCOM as of $surveydate :</H4></P>" -post "</p> This report generated by PowerShell, run on $runfrom by $runby." | Out-String
$Results -replace "(&lt;)(img href=)(")(.+?)(")( alt=)(")(.*?)(")(&gt;)", '<$2"$4"$6"$8">' > $ResultsHTML
That should fix anything so far as your image tags go. You may need to indicate a -width
on the Out-String
if your lines of HTML coding are extremely wide.
Upvotes: 3