比 Write-Host 更好的打印对象的方法
我使用 Write-Host 分析对象,但有时很难理解对象实际上是什么。
考虑:
Write-Host $null
Write-Host @()
Write-Host @($null, $null)
打印:
# Actually it prints nothing
我想要这样的东西:
Null
@()
@(Null, Null)
有什么建议吗?
I use Write-Host analyze objects, but some times it is hard to understand what the object actually is.
Consider:
Write-Host $null
Write-Host @()
Write-Host @($null, $null)
Prints:
# Actually it prints nothing
I would like some thing like this:
Null
@()
@(Null, Null)
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
对于这个特定的示例,您可以通过将它们粘贴到对象的属性中轻松获得您想要的东西。 作为示例,让我们创建一个包含三个测试的数组:
当然,
Out-String | Out-Host
的东西只是为了确保我们实际上不会将对象输出到管道,而是像 Write-Host 那样表现。现在我们可以运行我们的测试了:
当然,问题是它对于真实对象来说效果不太好,因为它将它们变成了对象的属性,它们被渲染为“ToString()”……但是,在我的脑海中,我无法想象如何在没有新对象的情况下调用在那里发生的渲染魔法。
For this specific example, you can easily get what you want by sticking them in the property of an object. For the sake of an example, lets create an array with your three tests in it:
Of course, the
Out-String | Out-Host
stuff is just to make sure we don't actually output the objects to the pipeline, but behave like Write-Host does.So now we can run our tests:
Of course, the problem with that is that it tends not to work so great for real objects because it's turning them into properties of an object, where they get rendered "ToString()" ... however, off the top of my head, I can't think how to invoke the rendering magic that happens there without the new object.
您可以编写一个函数来为您进行漂亮的打印。 类似下面的内容可能会满足您的需求:
但是,这对于 shell 上的空数组仍然存在问题(不过,在 .ps1 文件中工作正常)。 另外,不支持哈希表,但支持嵌套数组。 可能仍然需要一些管道,但可能会给出一个总体方向。
@($null, $null)
数组似乎是一个丑陋的野兽,甚至拒绝将其与$null
进行比较。 诡异的。You can write a function that does the pretty-printing for you. Something like the following might work for your needs:
This has, however still problems with an empty array on the shell (works fine from a .ps1 file, though). Also Hashtables aren't supported, but nested arrays are. Probably still needs some plumbing but might give a general direction.
The
@($null, $null)
array seems to be an ugly beast, resisting even comparing it to$null
. Weird.尝试做这样的事情将会遇到多个问题。
get-help about_format.ps1xml
或 MSDN PowerShell 格式化文件 页面。get-help get-host
或 TechNet 页面。 您需要了解至少三种常见主机:ConsoleHost
Windows PowerShell ISE 主机
PowerGUIScriptEditorHost
<块引用>
...
@( … )
操作是
[array] 的语法糖$( … )
所以 – 如果 @() 中的语句返回一个标量,它将被包装在一个数组中,但如果结果已经是一个数组,那么它就不会嵌套...
我很乐意能够为此提供一些代码,但它有点超出了此时的我...
There are going to be multiple issues trying to do something like this.
get-help about_format.ps1xml
, or from the MSDN PowerShell Formatting File pages.get-help get-host
for more info, or the TechNet page. There are AT LEAST three common hosts you want to be aware of:ConsoleHost
Windows PowerShell ISE Host
PowerGUIScriptEditorHost
I'd love to be able to provide some code for this, but it's a bit beyond me at this point...
对于空数组效果不佳,但您可以使用 format-xxx 命令之一。
这有助于格式化没有有用的 ToString 覆盖的对象。
例如
Doesn't work well for the empty arrays, but you could use one of the format-xxx commands.
This helps for formatting objects that don't have a helpful ToString override.
E.g.
你可以试试这个:
You could try this: