powershell - 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!
解决方案
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.
推荐阅读
- c++ - 在我定义的类中释放分配的缓冲内存的问题
- vb.net - 尝试在 DataGridView 上添加新行时出现 System.InvalidOperationException
- swift - 致命错误:SwiftUI 中的索引超出范围
- c# - 在某些尺寸上生成动态图像失败
- amazon-web-services - 没有地址的 EKS 入口 - 尝试测试我的 EKS 集群
- javascript - Discord.js 机器人,如何在没有引脚的情况下清除?
- javascript - 如何使用我的 svelte firebase + sendgrid 电子邮件功能修复 500 错误
- python-2.7 - pip install 在命令行中不返回任何内容
- git - 列出 a 和 b 之间的 Git 提交,仅考虑 b 的第一父母和 a 的第一父母
- javascript - 自动生成 Hreflangs