首页 > 解决方案 > PowerShell cast to void and return value

问题描述

I have a function that is running some code and return $True in case of success and $False otherwise.

As part of the function code I cast results of some cmdlets to [void] using this

function New-SampleFunction
{
    [void]::('2');

     return $true
}

[bool]$myReturnValuen = New-SampleFunction

# This results in exception
The above will result in a Cannot convert value "System.Object[]" to type "System.Boolean". Boolean parameters accept only Boolean values and numbers, such as $True, $False, 1 or 0.

Of course if I let PowerShell infer the type in function return value this will work but the funny part is what I get examining the variable

function New-SampleFunction
{
    [void]::('2')

     return $true
}

$myReturnValuen = New-SampleFunction


$returnValue.GetType()

IsPublic IsSerial Name                                     BaseType                                                                                                                           
-------- -------- ----                                     --------                                                                                                                           
True     True     Object[]                                 System.Array

But if I run $returnValue to Get-Member what I get is this

$returnValue | Get-Member


TypeName: System.Boolean

Issue is easily solved changing the above code to

function New-SampleFunction
{
    [void]('2')

     return $true
}

$myReturnValuen = New-SampleFunction

Issue in the code is resolved but I am now curious on why this behaving like this, I am of course missing something on the difference between the two void and [void]::(blahblah) and could not find any obvious answer to this.

Probaly is something really silly that I'm overlooking (had a really rough week...) but would really appreciate if somebody could enlightening me.

Thanks!

标签: powershellnullvoid

解决方案


The simplest form to suppress a return value it to use $null = ...:

$null = 2

Other, in most cases less desirable options include 2 >$null, 2 | Out-Null, [void] (2)[1].


As for what you tried:

[void]::('2')

tries to access a static member (::) of type [void] named 2, which is the name that expression ('2') evaluates to - which is clearly not your intent.

Since the [void] type has no such static member (property or method), $null is effectively returned[2] - and $null does get output to the pipeline, so your function has 2 outputs, $null and $true.

If you capture that in a variable, you'll get a 2-element [object[]] array - $null, $true - which explains your symptom.


[1] Interestingly, trying to use [void] (...), as part of an expression causes an error in Windows PowerShell generates an error in Windows PowerShell (e.g., ([void] 1), 2), whereas in PowerShell Core it is now more sensibly treated as $null (verify with $null -eq (([void] 1), 2)[0]).

[2] Note that you can prevent such inadvertent method / property references by calling Set-StrictMode -Version 2 or higher. You would then get the following statement-terminating error:

The property '2' cannot be found on this object. Verify that the property exists.


推荐阅读