tags:

views:

76

answers:

1

Is it possible to know the return value prepared by powershell before returning from function?

Pseudo code:

function Foo()
{
    1
    2
    Write-Host $CURRENT_RESULT # I would like it to print [1, 2]
    3
    4
    Write-Host $CURRENT_RESULT # I would like it to print [1, 2, 3, 4]
}
+2  A: 

There isn't a variable like $CURRENT_RESULT that I am aware of. You could do something manual/ugly like this:

function Foo 
{
    $OFS = ','
    $r1 = & {
        1
        2
    }
    $r1
    Write-Host "Returning $r1"
    $r2 = & {
        3
        4
    }
    $r2
    Write-Host "Returning $r2"
}

You also mention wanting to know the values before the function returns. Note that if you run this function and don't capture its output, 1 and 2 show up before the first call to Write-Host e.g:

PS> Foo
1
2
Returning 1,2
3
4
Returning 3,4

This is because functions behave like cmdlets i.e. they write to the output as soon as output becomes available. So in this regard, PowerShell functions don't quite behave like a traditional function.

Keith Hill
I also found nothing.
alex2k8