powershell - 在 Powershell 中输出具有颜色和背景颜色的对象
问题描述
我有一个名为的对象$myObject
:
@{attr1=My first attribut;attr2=My second attribut;....}
当我只使用对象名称时,对象会正确显示:
PS > $myObject
attr1 : My first attribut
attr2 : My second attribut
我想用特定颜色显示我的对象。我尝试使用 write-host重现示例 #4 。
背景和前景色有效,但我的对象显示为“对象”:
@{attr1=My first attribut;attr2=My second attribut;....}
我尝试了这样的多种组合:
$myObject | write-host -ForegroundColor DarkGreen
write-output $myObject -ForegroundColor DarkGreen
write-output $myObject | write-host -ForegroundColor DarkGreen
write-host $myObject -ForegroundColor DarkGreen
没有人工作。
知道怎么做吗?谢谢!
解决方案
Write-Host
接受一个输入对象,将其转换为字符串并将其写入主机应用程序的屏幕缓冲区 - 因此,如果您不希望它打印对象的默认字符串表示形式(即。@{prop=value;...}
),我们必须将其转换为在将其传递给Write-Host
.
要获得一个反映默认格式的字符串,PowerShell 会以其他方式应用于对象,请使用Out-String
:
Write-Host ($myObject |Out-String) -ForegroundColor DarkGreen
推荐阅读
- android-manifest - 有没有办法将 WAKE_LOCK 添加到 TWA?
- r - 如何在闪亮的情况下正确呈现 reactiveValue 的结果?
- reactjs - React Router 和 this.props:this.props.location.state 未定义
- android - 如何从viewmodel运行xml中的函数?Android、Kotlin、数据绑定
- c# - 如何获取已用特定属性修饰的枚举成员列表
- elasticsearch - 弹性搜索任何查询执行平台
- php - 以编程方式删除 PHP 部分
- sql - 三个表的左连接
- excel - 在 VBA 中使用输入框作为过滤条件
- sql-server - 如何将多个参数编程到 SQL Server 存储过程的代码中