2

How to enable Out-GridView in a function.

I mean,

 "Hello" | Out-GridView

Works.

But if I have a simple function like this

function Count ([int]$times)
{
    for ($i=1; $i -le $times;$i++)
    {
        Write-Host $i
    }
}

Why calling Count 5 doest not support a pipe to Out-GridView?

4

1 回答 1

3

您遇到的问题是Write-Host根本不输出到管道。它直接写入屏幕。替换Write-HostWrite-Output它应该可以正常工作。

顺便说一句,Write-Output是默认设置,因此您可以使用:

function Count ([int]$times)
{
    for ($i=1; $i -le $times;$i++)
    {
        $i
    }
}

或者更简单地说:

function Count([int]$times) 
{
    1..$times
}
于 2012-06-16T06:16:02.937 回答