如果數組只有一個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"