PowerShell ConvertTo-HTML is translating < and > symbols to &lt; and &gt; - how do I stop this?

StackOverflow https://stackoverflow.com/questions/23143393

  •  05-07-2023
  •  | 
  •  

Frage

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>&amp;lt;img href=&quot;greystate\0-5.png&quot; alt=&quot;5-15 Days&quot;&amp;gt;</td><td></td><td></td></tr>
....

Note the section above: &lt;img href="greystate\0-5.png" alt="5-15 Days"&gt;

How do I get ConvertTo-HTML to leave my tag punctuation alone so that it will render properly in the browser?

War es hilfreich?

Lösung

Well, one solution would be to pipe your HTML to Out-String, then run a RegEx replace on that string to convert ><td>&amp;lt;img href=&quot;greystate\0-5.png&quot; alt=&quot;5-15 Days&quot;&amp;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 "(&amp;lt;)(img href=)(&quot;)(.+?)(&quot;)( alt=)(&quot;)(.*?)(&quot;)(&amp;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.

Andere Tipps

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

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top