如果数组只有一个CustomObjects并且Count属性为null。 为什么?使用pscustomobjects统计PowerShell中的数组的属性
如果仅使用字符串,则Count属性为1。
function MyFunction
{
$Objects = @()
$Objects += [pscustomobject]@{Label = "Hallo"}
# $Objects += [pscustomobject]@{Label = "World"}
$Objects
}
$objs = MyFunction
Write-Host "Count: $($objs.Count)"
输出:"Count: "
因为$objs.Count
是null
function MyFunction
{
$Objects = @()
$Objects += [pscustomobject]@{Label = "Hallo"}
$Objects += [pscustomobject]@{Label = "World"}
$Objects
}
$objs = MyFunction
Write-Host "Count: $($objs.Count)"
输出:"Count: 2"
行为是不同的,如果我添加字符串
function MyFunction
{
$Objects = @()
$Objects += "Hallo"
# $Objects += [pscustomobject]@{Label = "World"}
$Objects
}
$objs = MyFunction
Write-Host "Count: $($objs.Count)"
输出:"Count: 1"