PowerShell ConvertTo-HTML正在翻译<和>符号到<和> - 我怎么阻止这个?

时间:2014-04-17 21:02:36

标签: powershell powershell-v2.0

我有一个有趣的问题。我尝试在PowerShell脚本中的字符串变量中嵌入HTML标记,以便在" down"中找到服务器。 state,当输出到HTML报告页面时,它将显示图形而不是数字。但是,当我打开报告而不是图形时,我会看到图形的HTML代码。这当然不会。

我的代码段如下:

$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

这应该添加一个图像标签,文件名存储在$ graphic中,alt文本存储在$ triage中(代码实际上通过If..Elseif..Else函数来改变细节,但你明白了)

然而,输出是这样的:

  

OFFLINESERVER.FQDN MG3 4/17/2014 4:56:03 AM TimedOut OFFLINESERVER 0&lt; img href =&#34; greystate \ 0-5.png&#34; alt =&#34; 5-15天&#34;&gt;

HTML页面的来源如下所示:

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

请注意以上部分:&amp; lt; img href =&#34; greystate \ 0-5.png&#34; alt =&#34; 5-15天&#34;&amp; gt;

如何让ConvertTo-HTML单独保留标签标点,以便在浏览器中正确呈现?

2 个答案:

答案 0 :(得分:2)

好吧,一个解决方案是将HTML管道传输到Out-String,然后在该字符串上运行RegEx替换,将><td>&amp;lt;img href=&quot;greystate\0-5.png&quot; alt=&quot;5-15 Days&quot;&amp;gt;</td><转换回><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

这应该可以解决您的图像标签的任何问题。如果您的HTML编码行非常宽,则可能需要在-width上指明Out-String

答案 1 :(得分:0)

在脚本中使用这两行来解决此错误

添加类型-AssemblyName System.Web

[System.Web.HttpUtility] :: HtmlDecode($ Results)|外档。\ Result.html