如何在PowerShell中测试$ null数组

Mar*_*rry 44 powershell null

我在PowerShell 2.0中使用了一个数组变量.如果没有值,则为$ null,我可以成功测试:

PS C:\> [array]$foo = $null
PS C:\> $foo -eq $null
True
Run Code Online (Sandbox Code Playgroud)

但是当我给它一个值时,$ null的测试不会返回任何内容:

PS C:\> [array]$foo = @("bar")
PS C:\> $foo -eq $null
PS C:\>
Run Code Online (Sandbox Code Playgroud)

"-eq $ null"怎么没有结果?它要么是$ null,要么不是.

确定是否填充数组与$ null相关的正确方法是什么?

Tay*_*ird 70

它是一个数组,所以你正在寻找Count来测试内容.

我推荐

$foo.count -gt 0
Run Code Online (Sandbox Code Playgroud)

这个"为什么"与PSH如何处理集合对象的比较有关

  • 但是(没有-gt 0 == FALSE),这就是我建议使用该运算符的原因.如果你检查你是否有真/假,无论是否为零 (4认同)
  • 如果你真的想伤害你的头脑,powershell会告诉你$ null -lt 0 == TRUE,但$ null -lt -1 == FALSE.所以-1 <null <0 (3认同)
  • 如果您具有Set-StrictMode -Version Latest,则此方法将失败 (3认同)
  • 不幸的是,当[[array] $ foo = $ null`时,这不起作用.而不是0,`$ foo.count`什么都不返回. (2认同)

Joe*_*oey 39

您可以重新排序操作数:

$null -eq $foo
Run Code Online (Sandbox Code Playgroud)

请注意,-eq在PowerShell中不是等价关系.


Joh*_*don 20

if($foo -eq $null) { "yes" } else { "no" }

help about_comparison_operators 
Run Code Online (Sandbox Code Playgroud)

显示帮助并包含以下文本:

除了包含运算符(-contains,-notcontains)和类型运算符(-is,-isnot)之外的所有比较运算符在运算符的输入(运算符左侧的值)是单个值时返回一个布尔值(一个标量).当输入是值的集合时,包含运算符和类型运算符返回任何匹配的值.如果集合中没有匹配项,则这些运算符不会返回任何内容.包含运算符和类型运算符始终返回一个布尔值.


Ant*_*ace 8

如果您的解决方案需要返回0而不是true/false,我发现这很有用:

PS C:\> [array]$foo = $null
PS C:\> ($foo | Measure-Object).Count
0
Run Code Online (Sandbox Code Playgroud)

此操作与数组的count属性不同,因为它Measure-Object是计算对象.由于没有,它将返回0.


BAC*_*CON 8

其他答案解决了问题的主旨,但只是对这部分进行评论......

PS C:\> [array]$foo = @("bar")
PS C:\> $foo -eq $null
PS C:\>
Run Code Online (Sandbox Code Playgroud)

“-eq $null”怎么会没有结果呢?它要么是 $null,要么不是。

一开始很令人困惑,但这了你 的结果$foo -eq $null,只是结果没有可显示的表示形式。

由于$foo保存一个数组,意味着“返回一个包含等于的$foo -eq $null元素的数组”。其中是否有任何元素等于?不,所以应该返回一个空数组。这正是它的作用,问题是当控制台上显示空数组时,您会看到...什么也没有...$foo$null$foo$null$foo -eq $null

PS> @()
PS> 
Run Code Online (Sandbox Code Playgroud)

即使您看不到它的元素,数组仍然存在......

PS> @().GetType()

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


PS> @().Length
0
Run Code Online (Sandbox Code Playgroud)

我们可以使用类似的命令来确认正在$foo -eq $null 返回一个我们无法“看到”的数组......

PS> $foo -eq $null
PS> ($foo -eq $null).GetType()

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


PS> ($foo -eq $null).Length
0
PS> ($foo -eq $null).GetValue(0)
Exception calling "GetValue" with "1" argument(s): "Index was outside the bounds of the array."
At line:1 char:1
+ ($foo -eq $null).GetValue(0)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : IndexOutOfRangeException
Run Code Online (Sandbox Code Playgroud)

请注意,我调用该Array.GetValue方法而不是使用索引器(即($foo -eq $null)[0]),因为后者返回$null无效索引,并且无法将它们与恰好包含 的有效索引区分开来$null

如果我们测试包含$null元素的数组/针对包含元素的数组,我们会看到类似的行为$null......

PS> $bar = @($null)
PS> $bar -eq $null
PS> ($bar -eq $null).GetType()

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


PS> ($bar -eq $null).Length
1
PS> ($bar -eq $null).GetValue(0)
PS> $null -eq ($bar -eq $null).GetValue(0)
True
PS> ($bar -eq $null).GetValue(0) -eq $null
True
PS> ($bar -eq $null).GetValue(1)
Exception calling "GetValue" with "1" argument(s): "Index was outside the bounds of the array."
At line:1 char:1
+ ($bar -eq $null).GetValue(1)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : IndexOutOfRangeException
Run Code Online (Sandbox Code Playgroud)

在这种情况下,$bar -eq $null返回一个包含一个元素的数组,$null该元素在控制台上没有视觉表示...

PS> @($null)
PS> @($null).GetType()

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


PS> @($null).Length
1
Run Code Online (Sandbox Code Playgroud)