Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clear captured output/return value in a Powershell function

Tags:

powershell

Is there a way to clear the return values inside of a function so I can guarantee that the return value is what I intend? Or maybe turn off this behaviour for the function?

In this example, I expect the return value to be an ArrayList containing ("Hello", "World")

function GetArray() 
{
    # $AutoOutputCapture = "Off" ?
    $myArray = New-Object System.Collections.ArrayList
    $myArray.Add("Hello")
    $myArray.Add("World")

    # Clear return value before returning exactly what I want?
    return $myArray
}

$x = GetArray
$x

However, the output contains the captured value from the Add operations.

0
1
Hello
World

I find this "feature" of Powershell very annoying because it makes it really easy to break your function just by calling another function, which you didn't know returned a value.

Update

I understand there are ways to prevent the output from being captured (as described in one of the answers below), but that requires you to know that a function actually returns a value. If you're calling another Powershell function, it can be, in the future, someone changes this function to return a value, which will then break your code.

like image 793
Mas Avatar asked Jul 03 '14 08:07

Mas


People also ask

How do you return a value from a function in PowerShell?

The return keyword exits a function, script, or script block. It can be used to exit a scope at a specific point, to return a value, or to indicate that the end of the scope has been reached.

What does void do in PowerShell?

Typically, PowerShell uses square brackets for only two things: In an array index ($services[0]) and to denote a data type. In this case, both sets of square brackets are being used to denote data types. The first set, [void], are being used to cast a result.

How do you return a value from a function?

To return a value from a function, you must include a return statement, followed by the value to be returned, before the function's end statement. If you do not include a return statement or if you do not specify a value after the keyword return, the value returned by the function is unpredictable.

How do you return an array from a function in PowerShell?

First, use the array sub-expression operator @( ... ). This operator returns the result of one or more statements as an array. If there is only one item, the array has only one member. Use Write-Output with the switch -NoEnumerate to prevent PowerShell from un-rolling the array.


1 Answers

I ran into the same issue. In PS a function would return all output pipe information. It gets tricky to | Out-Null rest of the code in the function. I worked around this by passing return variable as a reference parameter [ref] . This works consistently. Check the sample code below. Note: some of the syntax is important to avoid errors. e.g. parameter has to be passed in brackets ([ref]$result)

function DoSomethingWithFile( [ref]$result, [string]$file )
{
    # Other code writing to output
    # ....

    # Initialize result
    if (Test-Path $file){
        $result.value = $true
    } else {
        $result.value = $false
    }
}
$result = $null
DoSomethingWithFile ([ref]$result) "C:\test.txt" 
like image 135
Feru Avatar answered Sep 18 '22 03:09

Feru