如何在 PowerShell 中测试 $null 数组 [英] How to test for $null array in PowerShell
本文介绍了如何在 PowerShell 中测试 $null 数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在 PowerShell 2.0 中使用数组变量.如果它没有值,它将是 $null,我可以成功测试:
I'm using an array variable in PowerShell 2.0. If it does not have a value, it will be $null, which I can test for successfully:
PS C:\> [array]$foo = $null
PS C:\> $foo -eq $null
True
但是当我给它一个值时,$null 的测试没有返回任何东西:
But when I give it a value, the test for $null does not return anything:
PS C:\> [array]$foo = @("bar")
PS C:\> $foo -eq $null
PS C:\>
-eq $null"怎么可能没有结果?它要么是 $null,要么不是.
How can "-eq $null" give no results? It's either $null or it's not.
确定数组是否填充与 $null 的正确方法是什么?
What is the correct way to determine if an array is populated vs. $null?
推荐答案
这是一个数组,因此您正在寻找 Count 来测试内容.
It's an array, so you're looking for Count to test for contents.
我会推荐
$foo.count -gt 0
为什么"这与PSH如何处理集合对象的比较有关
The "why" of this is related to how PSH handles comparison of collection objects
这篇关于如何在 PowerShell 中测试 $null 数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文