• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Return values from functions

J

Joachim Meyer

#1
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 Computer

V

Vadims Podans [MVP]

#2
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" <[email protected]> rakstÄ«ja ziņojumÄ
"news:[email protected]"...

> 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 Computer

J

Joachim Meyer

#3
"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 Computer

V

Vadims Podans [MVP]

#4
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" <[email protected]> rakstÄ«ja ziņojumÄ
"news:[email protected]"...

> "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 Computer

J

Joachim Meyer

#5
"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 Computer

A

Alex K. Angelopoulos

#6
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" <[email protected]> wrote in message
news:[email protected]

> "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 Computer

J

Joachim Meyer

#7
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 Computer