标签: automation-null

为什么以及这两个$ null值有何不同?

显然,在PowerShell(第3版)中并非所有$null都是相同的:

    >function emptyArray() { @() }
    >$l_t = @() ; $l_t.Count
0
    >$l_t1 = @(); $l_t1 -eq $null; $l_t1.count; $l_t1.gettype()
0
IsPublic IsSerial Name                                     BaseType                                                         
-------- -------- ----                                     --------                                                         
True     True     Object[]                                 System.Array                                                     
    >$l_t += $l_t1; $l_t.Count
0
    >$l_t += emptyArray; $l_t.Count
0
    >$l_t2 = emptyArray; $l_t2 -eq $null; $l_t2.Count; $l_t2.gettype()
True
0
You cannot call a method on a null-valued expression.
At line:1 char:38
+ $l_t2 = emptyArray; $l_t2 -eq $null; $l_t2.Count; $l_t2.gettype()
+                                      ~~~~~~~~~~~~~~~
  + CategoryInfo …
Run Code Online (Sandbox Code Playgroud)

powershell null automation-null

10
推荐指数
2
解决办法
738
查看次数

Are there 2 kinds of $null in PowerShell?

I've stumbled across an odd situation in PowerShell 5.1 (Windows 10) where ConvertTo-Json produces {} instead of null for an apparently null valued object:

[CmdletBinding()]
param()
function main() {
    $foo1 = $null
    Write-Verbose ("foo1 null? " + ($null -eq $foo1))
    $foo2 = try{1/0}catch{}
    Write-Verbose ("foo2 null? " + ($null -eq $foo2))
    $foo3 = try{1/0}catch{$null}
    return [PSCustomObject]@{"foo1"=$foo1; "foo2" = $foo2; "foo3" = $foo3} | ConvertTo-Json
}
Set-StrictMode -Version Latest
$ErrorActionPreference = "Stop"
main
Run Code Online (Sandbox Code Playgroud)

Running this cmdlet -verbose shows two apparently $null …

powershell null json automation-null

7
推荐指数
0
解决办法
75
查看次数

标签 统计

automation-null ×2

null ×2

powershell ×2

json ×1