仅打印第一个对象

仅打印第一个对象

本文介绍了PowerShell写入输出缺少的信息-仅打印第一个对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1.ps1具有创建和打印的2个对象.但是输出中只显示一个.在以下情况下,两个对象都会出现:
(1)写输出是通过Format-List
完成的(2)object1具有四个以上的属性(因此它会自动进行垂直格式化)
试图了解这种行为背后的原因.

1.ps1 has 2 objects created and printed. But only one show up in the output.Both objects show up in the following cases:
(1) write-output is done with Format-List
(2) object1 has more than 4 properties (and so it gets vertically formatted automatically)
Trying to understand the reasoning behind this behaviour.

PS C:\> cat .\1.ps1
$object1 = New-Object PSObject
$object1 | add-member NoteProperty -name pn1 -value pv1
$object1 | add-member NoteProperty -name pn2 -value pv2
$object1 | add-member NoteProperty -name pn3 -value pv3
write-output $object1

$object2 = New-Object PSObject
$object2 | add-member NoteProperty -name npn1 -value npv1
$object2 | add-member NoteProperty -name npn2 -value npv2
$object2 | add-member NoteProperty -name npn3 -value npv3
$object2 | add-member NoteProperty -name npn4 -value npv4
$object2 | add-member NoteProperty -name npn5 -value npv5
$object2 | add-member NoteProperty -name npn6 -value npv6
write-output $object2
PS C:\>
PS C:\> .\1.ps1

pn1                                     pn2                                     pn3
---                                     ---                                     ---
pv1                                     pv2                                     pv3


PS C:\>

推荐答案

如果您只是尝试显示信息,则应使用 Write-Host ,而不是 Write-Output .在管道中发送数据时,您想使用 Write-Output ,而不能仅用于显示数据.

If you are just trying to display the information, you should be using Write-Host rather than Write-Output. You want to use Write-Output when you are sending data on in the pipeline, and shouldn't be used to simply display data.

这里是提供更深入答案的帖子:

Here is a post that gives a more in-depth answer: Which should I use: "Write-Host", "Write-Output", or "[console]::WriteLine"?

通过链接:

这篇关于PowerShell写入输出缺少的信息-仅打印第一个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 20:02