Powershell cmdlet输出未显示

时间:2013-01-14 20:56:02

标签: powershell

考虑脚本“test.ps1:

Get-EventLog -list | gm

从Powershell控制台(“。\ test.ps1”)调用它时,它会按预期输出成员。

但是,以下脚本不显示“Get-EventLog -list | gm”的输出:

脚本1

Get-EventLog -list | gm
break

脚本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) 

如果我将“Get-EventLog -list | gm”更改为“Hello World”这样的文本,则会在两种情况下都正确显示。

为什么cmdlet的输出未显示?

1 个答案:

答案 0 :(得分:0)

我看了一些有关powershell流水线的文章。使用的cmdlet返回一组对象,powershell的default behavior用于将输出转发到屏幕。

This article解释说break语句会破坏管道(事实上,它会在执行父循环之前中断执行),从而解释第一个例子的行为。

至于第二个例子,我假设powershell将默认在脚本执行结束时重定向到控制台。由于$ host.ui.PromptForChoice不使用第一个cmdlet调用的输出,并生成它自己的,因此Get-EventLog的结果将被丢弃。

就目前而言,正如@Christian所说,总是使用“Out-Host”是the way to go