请尝试以下操作:
$servers= 'GT544', 'HT54765', 'J4356', 'SW5654', 'JY67432'
$servers | ConvertTo-Html -Property @{ l='Name'; e={ $_ } }
注:AS
EBGreen
注意到,
$servers
变量不应被类型约束为
[string] $servers = ...
,因为它将字符串数组转换为
单一的
字符串。在这种情况下,不需要严格地键入constraint,但是可以使用
[string[]] $servers = ...
ConvertTo-Html
默认情况下枚举所有
性质
在输入对象的情况下,
[string]
只有
.Length
财产。
因此您需要使用
计算属性
那个
包裹
你的琴弦,这就是
@{ l='Name'; e={ $_ } }
确实;
l
条目为您的属性
名称
(将在表头中使用),以及
e
条目定义其
价值
通过脚本块(
{ ... }
,在本例中,它只是输入字符串本身(
$_
)
有关计算属性的详细信息,请参见
this answer
但请注意
转换为HTML
奇怪的是只支持
L
/
label
用于命名属性的键(不包括
n
/
name
)
另外,传递计算属性是
currently broken altogether in PowerShell
Core
, as of v6.1.0-preview.2
上述结果:
HTML TABLEName |
---|
GT544 |
HT54765 |
J4356 |
SW5654 |
JY67432 |
总而言之:
$servers = 'GT544', 'HT54765', 'J4356', 'SW5654', 'JY67432'
$file = "C:\Users\karljoey.chami\Desktop\Htmltesting\Result.html"
$servers | ConvertTo-Html -Property @{ l='Name'; e={ $_ } } -Title "Servers in a table" `
-CssUri "C:\Users\karljoey.chami\Desktop\Htmltesting\Red.css" `
-pre "The servers are" | Out-file $file
Invoke-Item $file