PSObject转换为带头的ConvertTo-Html?

时间:2014-11-18 20:55:36

标签: html powershell

以下powershell脚本生成*作为列标题。为什么不XXX

$a = "a","b","c"
$a | select -Property @{Name="XXX"; Expression={$_}} | ConvertTo-Html

如何使列标题为" XXX"?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>HTML TABLE</title>
</head><body>
<table>
<colgroup><col/></colgroup>
<tr><th>*</th></tr> <!--- <------ * ----->
<tr><td>a</td></tr>
<tr><td>b</td></tr>
<tr><td>c</td></tr>
</table>
</body></html>

2 个答案:

答案 0 :(得分:1)

答案 1 :(得分:0)

我知道这已经过时了,但如果有其他人像我一样发生这件事,那么这对我有用。

选项一:

$a = "a","b","c"
$a | ForEach-Object {$_ | Add-Member -Type NoteProperty -Name XXX -Value $_; $_} | ConvertTo-Html -Property XXX

哪个给出了

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>HTML TABLE</title>
</head><body>
<table>
<colgroup><col/></colgroup>
<tr><th>XXX</th></tr>
<tr><td>a</td></tr>
<tr><td>b</td></tr>
<tr><td>c</td></tr>
</table>
</body></html>

选项二

另一种方式,根据数组是构建还是静态定义,可能有效也可能无效,是使用对象构建数组。如果您自己构建阵列,使用以下内容也会使生成html文件更加简单:

$a += New-Object PSObject -Property @{XXX=$value}
$a | ConvertTo-Html -Property XXX

收率:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>HTML TABLE</title>
</head><body>
<table>
<colgroup><col/></colgroup>
<tr><th>XXX</th></tr>
<tr><td>a</td></tr>
<tr><td>b</td></tr>
<tr><td>c</td></tr>
</table>
</body></html>

不确定哪个适用于OP的3岁问题,但这些都适用于PSv2和3.希望有人能从这一切中找到一些用处!