Return values from functions


  1.    02 Apr 2009 #1
    Joachim Meyer Guest

    Return values from functions


    I usually like PowerShell very much but there is one feature I absolutely
    hate: PowerShell returns any output within a function.

    I have the following function:

    1 function ExtractVersionNumber($Collection) {
    2 if ($Collection -match "V\d{1,2}") {
    3 $matches[0] -match "\d+"
    4 $matches[0]
    5 }
    6 else { 0 }
    7 }

    Unfortunately, the statement in line 3 produces an output but if I want to
    omit the output using [void] it still generates an output and thus messes up
    my return value. The return value should be the output of either line 4 or
    line 6.

    Any ideas?

    Thanks,
    Joachim
      My System SpecsSystem Spec

  2.    02 Apr 2009 #2
    Vadims Podans [MVP] Guest

    Re: Return values from functions


    use Out-Null in pipeline:

    $matches[0] -match "\d+" | out-null

    this will avoid any output from command.
    --
    WBR, Vadims Podans
    MVP: PowerShell
    PowerShell blog - www.sysadmins.lv

    "Joachim Meyer" <joachim.me@xxxxxx> rakstīja ziņojumā
    "news:9670BD64-3839-451B-AD11-D77BAA872CC6@xxxxxx"...

    > I usually like PowerShell very much but there is one feature I absolutely
    > hate: PowerShell returns any output within a function.
    >
    > I have the following function:
    >
    > 1 function ExtractVersionNumber($Collection) {
    > 2 if ($Collection -match "V\d{1,2}") {
    > 3 $matches[0] -match "\d+"
    > 4 $matches[0]
    > 5 }
    > 6 else { 0 }
    > 7 }
    >
    > Unfortunately, the statement in line 3 produces an output but if I want to
    > omit the output using [void] it still generates an output and thus messes
    > up
    > my return value. The return value should be the output of either line 4 or
    > line 6.
    >
    > Any ideas?
    >
    > Thanks,
    > Joachim
      My System SpecsSystem Spec

  3.    02 Apr 2009 #3
    Joachim Meyer Guest

    Re: Return values from functions


    "Vadims Podans [MVP]" wrote:

    > use Out-Null in pipeline:
    >
    > $matches[0] -match "\d+" | out-null
    >
    > this will avoid any output from command.
    Thanks, I thought out-null is equivalent with [void] but apparently I was
    wrong.

    Regards,
    Joachim
      My System SpecsSystem Spec

  4.    02 Apr 2009 #4
    Vadims Podans [MVP] Guest

    Re: Return values from functions


    no, they're not equivalent. In fact Out-Null is the same as redirection to
    $null: "statement > $null". Also Out-Null help us with running external
    programs in powershell. For example, if you use ntbackup CLI to perform
    scheduled backups powershell won't wait until ntbackup finish his job.
    Therefore you will unable to check that backup finished successfully. And
    using Out-null after ntbackup CLI command - powershell run ntbackup job and
    will wait until it finishes backup job and after this continues other
    commands in PS script.
    --
    WBR, Vadims Podans
    MVP: PowerShell
    PowerShell blog - www.sysadmins.lv

    "Joachim Meyer" <joachim.me@xxxxxx> rakstīja ziņojumā
    "news:9A10E914-C7E9-4C81-A2F8-4BEC3230785D@xxxxxx"...

    > "Vadims Podans [MVP]" wrote:
    >

    >> use Out-Null in pipeline:
    >>
    >> $matches[0] -match "\d+" | out-null
    >>
    >> this will avoid any output from command.
    >
    > Thanks, I thought out-null is equivalent with [void] but apparently I was
    > wrong.
    >
    > Regards,
    > Joachim
      My System SpecsSystem Spec

  5.    02 Apr 2009 #5
    Joachim Meyer Guest

    Re: Return values from functions


    "Vadims Podans [MVP]" wrote:

    > no, they're not equivalent. In fact Out-Null is the same as redirection to
    > $null: "statement > $null".
    It just sounds as they would behave the same when reading Bruce Payette's
    book. A quote from it:

    "A corollary is that, by default, the return value of a statement is not
    voided. This
    means that if we call a method that returns a value we aren’t going to use,
    we have to explicitly discard it, either by casting it to [void] or
    redirecting output to $null."

    Thanks for your hint.

    Joachim
      My System SpecsSystem Spec

  6.    03 Apr 2009 #6
    Alex K. Angelopoulos Guest

    Re: Return values from functions


    Joachim, how were you using the [void]? You didn't show that, and I suspect
    that you applied it like this:

    [void]$matches[0] -match "\d+"

    which voids $matches[0] by itself. When I run your code using this:

    [void]($matches[0] -match "\d+")

    it completely suppresses the output from that line.

    Although a cast to void and piping to null are not the same thing, in terms
    of output suppression as Bruce was talking about, the void cast should
    indeed work correctly when applied to that statement.

    "Joachim Meyer" <joachim.me@xxxxxx> wrote in message
    news:85639670-9BC5-4986-83F0-5EF3B8EA65FD@xxxxxx

    > "Vadims Podans [MVP]" wrote:
    >

    >> no, they're not equivalent. In fact Out-Null is the same as redirection
    >> to
    >> $null: "statement > $null".
    >
    > It just sounds as they would behave the same when reading Bruce Payette's
    > book. A quote from it:
    >
    > "A corollary is that, by default, the return value of a statement is not
    > voided. This
    > means that if we call a method that returns a value we aren’t going to
    > use,
    > we have to explicitly discard it, either by casting it to [void] or
    > redirecting output to $null."
    >
    > Thanks for your hint.
    >
    > Joachim
      My System SpecsSystem Spec

  7.    05 Apr 2009 #7
    Joachim Meyer Guest

    Re: Return values from functions


    Alex,

    "Alex K. Angelopoulos" <aka(at)mvps.org> wrote:

    > Joachim, how were you using the [void]? You didn't show that, and I
    > suspect that you applied it like this:
    >
    > [void]$matches[0] -match "\d+"
    >
    > which voids $matches[0] by itself. When I run your code using this:
    >
    > [void]($matches[0] -match "\d+")
    >
    > it completely suppresses the output from that line.
    >
    > Although a cast to void and piping to null are not the same thing, in
    > terms of output suppression as Bruce was talking about, the void cast
    > should indeed work correctly when applied to that statement.
    You are right, of course I did not use the brackets around the expression.
    Thanks for your explanation.

    Joachim

      My System SpecsSystem Spec

Return values from functions

Similar Threads
Thread Forum
Re: Perform OR operation on function return values
On May 27, 5:05pm, "M" <m...@newsgroup> wrote: The function parameters need to be enclosed in parentheses, as in ... If...
VB Script
Script running other scripts return values
hello I've got series of VB scripts that I run manually buy clicking on them, Can't I have one script that runs all of them? I would want to...
VB Script
How to combine/return multiple values from a sql query
Hi guys. I've been stuck on this for a while as a newb, so please forgive me in advance. Any help would be very appreciated. I wrote a little...
VB Script
Associative Array - how to return mapped values?
Hello PowerShellers, How do I return a mapped value from an associative array? I have this books.csv file: ID,Author,Title 1,"Dr...
PowerShell
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. ...
PowerShell
Return of object with synthetic members from functions
Hi I have been unable to return objects with the synthetic members preserved from functions. The example below illustrates this. ----------...
PowerShell
function return values, console output
I'm trying to figure out how to run msbuild in a function and have this function return true or false indicating msbuild's success or failure. Also...
PowerShell

Our Sites
  • Ten Forums
  • Eight Forums
  • Seven Forums
  • Help Me Bake
  • Site Links
  • Contact Us
  • Privacy and Cookies
  • About Us
    Windows Vista Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

    Designer Media Ltd
    All times are GMT -5. The time now is 13:31.
    .