Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Returning Variables from a Function...

Reply
 
Old 01-30-2008   #1 (permalink)
rush


 
 

Returning Variables from a Function...

Is it possible to return 2 variables from a function?

Somethig like this.....?

Function DoThis ( $inString, $outString1, $outString2)
{
..... Do some stuff

$outString1 = $a
$outString2 = $b

}

DoThis $inputToFunction $outputFromFunction1 $outputFromFunction2

$outputFromFunction1
$outputFromFunction2

Does that make sense....!? doubt it.
Basically however it's done, I like to call a function that takes one
input, and returns 2 or 3 outputs to assigned variables.

Cheers
Russ.

My System SpecsSystem Spec
Old 01-30-2008   #2 (permalink)
Shay Levi


 
 

Re: Returning Variables from a Function...



Anything that is not captured (assigned to a variable) inside the function
is returned.

Check out
Effective PowerShell Item 7: Understanding "Output"
http://keithhill.spaces.live.com/Blo...3A97!811.entry


-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com
Quote:

> Is it possible to return 2 variables from a function?
>
> Somethig like this.....?
>
> Function DoThis ( $inString, $outString1, $outString2)
> {
> .... Do some stuff
> $outString1 = $a
> $outString2 = $b
> }
>
> DoThis $inputToFunction $outputFromFunction1 $outputFromFunction2
>
> $outputFromFunction1
> $outputFromFunction2
> Does that make sense....!? doubt it.
> Basically however it's done, I like to call a function that takes one
> input, and returns 2 or 3 outputs to assigned variables.
> Cheers
> Russ.

My System SpecsSystem Spec
Old 01-30-2008   #3 (permalink)
Brandon Shell [MVP]


 
 

Re: Returning Variables from a Function...

Actually you have to try to control what you return.

Function in Powershell are not like functions in VBScript.

In Powershell anything that is outputed will get returned from the function.

So lets take this example
function foo {"Value1","Value2"}
$v1,$v2 = foo
$v1
Value1
$v2
Value2

An exception is if you use Write-Host, Write-Verbose, Write-Error... These
write data to console and not output.

Make sense?

Brandon Shell
---------------
Blog: http://www.bsonposh.com/
PSH Scripts Project: www.codeplex.com/psobject

r> Is it possible to return 2 variables from a function?
r>
r> Somethig like this.....?
r>
r> Function DoThis ( $inString, $outString1, $outString2)
r> {
r> .... Do some stuff
r> $outString1 = $a
r> $outString2 = $b
r> }
r>
r> DoThis $inputToFunction $outputFromFunction1 $outputFromFunction2
r>
r> $outputFromFunction1
r> $outputFromFunction2
r> Does that make sense....!? doubt it.
r> Basically however it's done, I like to call a function that takes one
r> input, and returns 2 or 3 outputs to assigned variables.
r> Cheers
r> Russ.


My System SpecsSystem Spec
Old 01-30-2008   #4 (permalink)
Roman Kuzmin


 
 

Re: Returning Variables from a Function...

Try this:

help about_ref

Is it what you want?

--
Thanks,
Roman Kuzmin

http://code.google.com/p/farnet/
PowerShell and .NET in Far Manager


"rush" <russell.hancock@xxxxxx> wrote in message
news:01f7bd93-1beb-44d9-bed6-25c21241ea64@xxxxxx
Quote:

> Is it possible to return 2 variables from a function?
>
> Somethig like this.....?
>
> Function DoThis ( $inString, $outString1, $outString2)
> {
> .... Do some stuff
>
> $outString1 = $a
> $outString2 = $b
>
> }
>
> DoThis $inputToFunction $outputFromFunction1 $outputFromFunction2
>
> $outputFromFunction1
> $outputFromFunction2
>
> Does that make sense....!? doubt it.
> Basically however it's done, I like to call a function that takes one
> input, and returns 2 or 3 outputs to assigned variables.
>
> Cheers
> Russ.
My System SpecsSystem Spec
Old 01-30-2008   #5 (permalink)
Karl Prosser[MVP]


 
 

Re: Returning Variables from a Function...

as was mentioned everything not consumed in a function is returned..

so
function myfunc {
1
2
3
4
5
}

when you run that 5 items are returned..

if you do

$a = myfunc

then $a will contain an array with 5 itemes.. well powershell allows the
syntax of assigning the results to more than one item.. so

$a,$b,$c,$d,$e = myfunc

will put 1 in $a, 2 in $b etc

well what happens when there are more items returned than variables?
basically the last item gets the remaining

so
$a,$b,$c = myfunc

will result in $a containing 1
$b containg 2
and $c containing and array of 3 4 and 5.

-Karl
My System SpecsSystem Spec
Old 01-31-2008   #6 (permalink)
rush


 
 

Re: Returning Variables from a Function...

Thanks Karl & Brandon...
That's what I needed to know!
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Problem in returning an object from a function VB Script
function not returning anything PowerShell
Function returning a value VB Script
Can I get two variables out of a function? PowerShell
Parsing Text File and returning variables PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46