Reputation: 7058
Consider the script "test.ps1:
Get-EventLog -list | gm
When it is called from the Powershell console (".\test.ps1"), it outputs the members as expected.
However, the following scripts don't show the output from "Get-EventLog -list | gm":
Script 1
Get-EventLog -list | gm
break
Script 2
Get-EventLog -list | gm
$yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes", "Continua execucao"
$no = New-Object System.Management.Automation.Host.ChoiceDescription "&No", "Cancela operacao"
$options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)
$result = $host.ui.PromptForChoice("Title", "Message", $options, 0)
If I change "Get-EventLog -list | gm" to a text like "Hello World", it is correctly displayed, in both cases.
Why is the output from the cmdlet not shown?
Upvotes: 6
Views: 5407
Reputation: 7058
I took a look on some articles on powershell pipelining. The cmdlet used returns a collection of objects, and powershell's default behavior is to forward the output to the screen.
This article explains that the break statement will break the pipeline (in fact, it will break execution until it finds a parent loop), thus explaining the behavior of the first example.
As for the second example, I assume powershell will only default to redirecting to the console on the end of the script execution. As the $host.ui.PromptForChoice does not use the first cmdlet call's output, and produces it's own, the results from Get-EventLog are just discarded.
As it stands, always using "Out-Host", as @Christian said, is the way to go.
Upvotes: 4