Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return several items from a Powershell function

Tags:

powershell

In a script I have several functions that need to break down an SVN external definition. These can either be <url> <path>, -r<rev> <url> <path>, or -r <rev> <url> <path>, and the mechanics I came up with to extract the data is more than just two lines, so I want to put this into a separate function.

But when I do so, I end up with three variables containing the relevant data which are local to the function, and I see no way to get all three of them them to the caller.

In a proper programming language I would return a compound type containing all three values, but I don't see how to do that in Powershell. Of course, I could split this into three separate functions, but then I am back at violating the DRY rule.

So what can I do here in Powershell?

like image 471
sbi Avatar asked Sep 27 '12 11:09

sbi


People also ask

How do I return multiple values from a function in PowerShell?

To return multiple values from a PowerShell function, you can return an array of objects. The following example returns the multiple values from a function num using an array. Code: Copy function num() { $a = 4,5,6 return $a } $b=num Write-Host "The numbers are $($b[0]),$($b[1]),$($b[2])."

How do you return multiple things from a function?

We can return more than one values from a function by using the method called “call by address”, or “call by reference”. In the invoker function, we will use two variables to store the results, and the function will take pointer type data. So we have to pass the address of the data.


2 Answers

I agree with @Christian, and I add another solution.

First you can return using an array explicitly or implicitly :

A) explicitly

function ExplicitArray () {   $myArray = @()    $myArray += 12   $myArray += "Blue"    return ,$myArray }  Clear-Host $a = ExplicitArray Write-Host "values from ExplicitArray are $($a[0]) and $($a[1])" 

B) implicitly

function ImplicitArray () {   Write-Output 12    Write-Output "Blue"   return "green" }  $b = ImplicitArray Write-Host "values from ImplicitArray are $($b[0]), $($b[1]) and $($b[2])" 

Second you can return a custom object :

A) Short form

function ReturnObject () {   $value = "" | Select-Object -Property number,color   $value.Number = 12   $value.color = "blue"   return $value } $c = ReturnObject Write-Host "values from ReturnObject are $($c.number) and $($c.color)" 

B) School form

function SchoolReturnObject () {   $value = New-Object PsObject -Property @{color="blue" ; number="12"}   Add-Member -InputObject $value –MemberType NoteProperty –Name "Verb" –value "eat"   return $value } $d = SchoolReturnObject Write-Host "values from SchoolReturnObject are $($d.number), $($d.color) and $($d.Verb)" 

Third using argumen by reference

function addition ([int]$x, [int]$y, [ref]$R) {  $Res = $x + $y  $R.value = $Res }  $O1 = 1 $O2 = 2 $O3 = 0 addition $O1 $O2 ([ref]$O3) Write-Host "values from addition $o1 and $o2 is $o3" 
like image 71
JPBlanc Avatar answered Oct 01 '22 10:10

JPBlanc


Maybe I am misunderstanding the question but isn't this just as simple as returning a list of variables, and the caller simply assigns each to a variable. That is

> function test () {return @('a','c'),'b'} > $a,$b = test 

$a will be an array, and $b the letter 'b'

> $a  a c > $b b 
like image 20
eythort Avatar answered Oct 01 '22 08:10

eythort