【发布时间】:2020-06-29 21:08:57
【问题描述】:
所以我有一个对象
$appstatus=[pscustomobject] @{
txtlist=@()
csvlist=@()
someotherproperties
}
以及加载 TXT 或导入 CSV 文件的函数。根据选择的文件名,它会填充 $appstatus 对象的一个属性。然后我有另一个函数来显示当前加载的列表。类似的东西
function showhosts(){
if(($appstatus.txtlist).count -gt 0){
write-host $appstatus.txtlist
}else{
write-host $appstatus.csvlist
}
}
txtlist 很好,但问题出在 csvlist 上,因为 write-host 没有显示漂亮的表格格式,但是这个 @{property=value; ...} 长字符串。我不能在没有 write-host 的情况下只键入 $appstatus.csvlist,因为它不会被显示并成为函数的返回值,所以我怎样才能从函数中很好地显示对象,就像从函数中调用它一样主脚本?
【问题讨论】:
-
你试过
Format-Table吗? -
| ft?将管道格式化为表格 -
只要使用
Out-Host-->$appstatus.txtlist | out-host -
添加到AdminOfThings' helpful answer:
Write-Host执行简单的.ToString()字符串化(通常无用的单行表示),而Out-Host使用PowerShell 丰富的显示格式系统。 -
@JonathonAnderson
Write-Host输出无法通过管道传输,因为它没有进入 PowerShell 的(成功)输出流,它(有效地)直接进入主机(显示)。请注意,OP 明确需要 not 写入输出流。
标签: powershell