Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

How does return work in powershell functions

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 11-18-2007   #1 (permalink)
meissnersd
Guest


 

How does return work in powershell functions

Hey script quys.
I am trying to understand the return values for functions in Powershell

I defined a simple function in a script file.


function foo( [string] $ss ) {
$sb = new "System.Text.StringBuilder"
$sb.Append( $ss )
return $sb
}


I dot sourced it and then
Quote:

> . .\test.ps1
>$x = foo "abc"
>$x

Capacity
MaxCapacity Length
--------
----------- ------
16
2147483647 1
16
2147483647 1

$x is an array of objects.
First I returned the string builder, not an array....
Second the string builder is in the array twice.
Huh?

My System SpecsSystem Spec
Old 11-18-2007   #2 (permalink)
Shay Levi
Guest


 

Re: How does return work in powershell functions


"Function Output Consists of Everything That Isn't Captured".
For a deep understanding of your problem, I recommend reading Kieth Hill's
Effective PowerShell
posts series, start with Item 7: Understanding "Output", It has exactly what
you need.

http://keithhill.spaces.live.com/Blo...3A97!811.entry

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


Quote:

> Hey script quys.
> I am trying to understand the return values for functions in
> Powershell
> I defined a simple function in a script file.
>
> function foo( [string] $ss ) {
> $sb = new "System.Text.StringBuilder"
> $sb.Append( $ss )
> return $sb
> }
> I dot sourced it and then
>
Quote:

>> . .\test.ps1
>> $x = foo "abc"
>> $x
> Capacity
> MaxCapacity Length
> --------
> ----------- ------
> 16
> 2147483647 1
> 16
> 2147483647 1
> $x is an array of objects.
> First I returned the string builder, not an array....
> Second the string builder is in the array twice.
> Huh?

My System SpecsSystem Spec
Old 11-18-2007   #3 (permalink)
Keith Hill [MVP]
Guest


 

Re: How does return work in powershell functions

"meissnersd" <meissnersd@xxxxxx> wrote in message
news:09136729-2474-43E6-A302-797348A8DF9A@xxxxxx
Quote:

> Hey script quys.
> I am trying to understand the return values for functions in Powershell
>
This is a common problem that folks with more traditional programming
experience when using functions in shell languages like PowerShell (or Korn
shell). I have a series of blog posts that explain this issue. You can
read them here:

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

Effective PowerShell Item 8: Output Cardinality - Scalars, Collections and
Empty Sets - Oh My!
http://keithhill.spaces.live.com/blo...3A97!816.entry

--
Keith

My System SpecsSystem Spec
Old 11-21-2007   #4 (permalink)
meissnersd
Guest


 

Re: How does return work in powershell functions


Thanks Keith!
That was really helpful
Now I will have to read the rest of your blog :-)

btw I noticed the "vote for better loggin" links at the bottom of the blog
were pointing to dead pages.

I would add another option to vote for "typing 'help return' in powershell
tells you about this issue." Documentation on this seems pretty key.
Especially since it will surprise folks with programming backgrounds and
since you said a lot of people post this question over and over again.

My System SpecsSystem Spec
Old 11-21-2007   #5 (permalink)
Keith Hill [MVP]
Guest


 

Re: How does return work in powershell functions

"meissnersd" <meissnersd@xxxxxx> wrote in message
news:393DD2C5-E368-4066-8FB4-0328E9D61E93@xxxxxx
Quote:

>
> Thanks Keith!
> That was really helpful
> Now I will have to read the rest of your blog :-)
>
> btw I noticed the "vote for better loggin" links at the bottom of the blog
> were pointing to dead pages.
You need to establish a login on the Microsoft Connect site which just
requires logging in with a Windows Live ID (or .NET Passport ID). Then the
links will work.
Quote:

> I would add another option to vote for "typing 'help return' in
> powershell
> tells you about this issue." Documentation on this seems pretty key.
> Especially since it will surprise folks with programming backgrounds and
> since you said a lot of people post this question over and over again.
Once you are on the Connect site you should be able to submit bugs and
defects against PowerShell. If you feel particularly strongly about an
issue, post a link to the newsgroup and see if you can get other folks to
vote on it.

--
Keith

My System SpecsSystem Spec
Old 11-21-2007   #6 (permalink)
Bob Butler
Guest


 

Re: How does return work in powershell functions

"Keith Hill [MVP]" <r_keith_hill@xxxxxx_spam_I> wrote in message
news:4451264B-4BC4-42C1-87C4-175A450A41CF@xxxxxx
Quote:

> This is a common problem that folks with more traditional programming
> experience when using functions in shell languages like PowerShell (or
> Korn shell).
My suggestion for this would be to leave the default behaviour of returning
all non-captured values *unless* return statement is encountered. In that
case return only what is explicitly specified.

for example:
function noret {
1
2
3
}

returns an array of 3 values

function withret {
1
2
3
return 4
}

returns just the value 4

That would let the non-programmer users continue to get what they expect and
allow us anal-retentive programmer types to get what we expect from it as
well <g>

If it's too far gone for that now then add a "returnonly" keyword that
flushes the non-captured values. I can't tell you how much time I've lost
tracking down bugs because something returned a value that I hadn't expected
or cared about.

My System SpecsSystem Spec
Old 11-21-2007   #7 (permalink)
Keith Hill [MVP]
Guest


 

Re: How does return work in powershell functions

"Bob Butler" <noway@xxxxxx> wrote in message
news:evBJCTGLIHA.1212@xxxxxx
Quote:

> My suggestion for this would be to leave the default behaviour of
> returning all non-captured values *unless* return statement is
> encountered. In that case return only what is explicitly specified.
>
> for example:
> function noret {
> 1
> 2
> 3
> }
>
> returns an array of 3 values
>
> function withret {
> 1
> 2
> 3
> return 4
> }
>
> returns just the value 4
>
> That would let the non-programmer users continue to get what they expect
> and allow us anal-retentive programmer types to get what we expect from it
> as well <g>
>
> If it's too far gone for that now then add a "returnonly" keyword that
> flushes the non-captured values. I can't tell you how much time I've lost
> tracking down bugs because something returned a value that I hadn't
> expected or cared about.
Yeah I don't think they could change the default behavior now but I really
like the idea of a returnonly keyword. Post on the Connect site as a
suggestion and I'll vote for it.

http://connect.microsoft.com

--
Keith

My System SpecsSystem Spec
Old 11-21-2007   #8 (permalink)
Bob Butler
Guest


 

Re: How does return work in powershell functions

"Keith Hill [MVP]" <r_keith_hill@xxxxxx_spam_I> wrote in message
news:1453305D-2E9B-4396-B98C-DF478100506F@xxxxxx
Quote:

> Yeah I don't think they could change the default behavior now but I really
> like the idea of a returnonly keyword. Post on the Connect site as a
> suggestion and I'll vote for it.
>
> http://connect.microsoft.com
Sorry, no way in this lifetime I'll ever sign up for a Windows Live ID


My System SpecsSystem Spec
Closed Thread

Thread Tools
Display Modes



Similar Threads
Thread Thread Starter Forum Replies Last Post
Return Powershell Error to VBscript bchad PowerShell 1 06-09-2008 02:26 PM
Filters and Functions. Even PowerShell can't tell them apart? Joel (Jaykul) Bennett PowerShell 0 03-23-2008 08:15 PM
Can Powershell return Exchange mailbox info? Calaway PowerShell 5 01-16-2008 06:03 PM
Return of object with synthetic members from functions Knut T PowerShell 5 07-07-2007 07:26 PM
Invoking PowerShell functions with parameters from .NET: issues. Roman Kuzmin PowerShell 0 04-23-2007 07:35 AM


Update your Vista Drivers Update Your Vista Drivers Now!!

Vistax64.com 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 2005-2008