问题
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:\>
回答1:
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"?
From the link:
Write-Output should be used when you want to send data on in the pipe line, but not necessarily want to display it on screen. The pipeline will eventually write it to out-default if nothing else uses it first. Write-Host should be used when you want to do the opposite. [console]::WriteLine is essentially what Write-Host is doing behind the scenes.
回答2:
I can't explain this behavior, but you can mitigate it by using an explicit formatter for the first output, e.g.:
Write-Output $object1 | Format-Table
It looks like without explicit formatting PowerShell remembers an implicitly applied table format and omits output that doesn't match this format. For instance, if you add another Write-Output $object1 at the end of the script:
$object1 = New-Object PSObject
...
write-output $object1
$object2 = New-Object PSObject
...
write-output $object2
write-output $object1
you will get this output:
PS C:\> .\test.ps1
pn1 pn2 pn3
--- --- ---
pv1 pv2 pv3
pv1 pv2 pv3
Wat?! O_o
Why anyone would consider this a good idea is beyond me.
回答3:
I think it has to do with the way Format-Table works. I think by default PowerShell will either pick Format-Table or Format-List to display objects on the console, I'm not sure how the decision is made but obviously Format-Table was chosen.
Format-Table works by taking the first object and picking the properties from that. For any object that follows the first one it will only populate the columns for the properties from the first object.
You might want to try
.\1.ps1 | % { $_ | ft }
And see if that get's you what you are looking for.
来源:https://stackoverflow.com/questions/18021932/powershell-write-output-missing-information-prints-only-1st-object