使用 pscustomobjects 在 PowerShell 中计算数组的属性 [英] Count property of array in PowerShell with pscustomobjects

查看:48
本文介绍了使用 pscustomobjects 在 PowerShell 中计算数组的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果数组只有一个 CustomObjects 并且 Count 属性为空.为什么?

If the array has only one CustomObjects and the Count property is null. Why?

如果仅使用字符串,则 Count 属性为 1.

If use only strings, the Count property is one.

function MyFunction
{
    $Objects = @()
    $Objects += [pscustomobject]@{Label = "Hallo"}
    # $Objects += [pscustomobject]@{Label = "World"}

    $Objects
}

$objs = MyFunction
Write-Host "Count: $($objs.Count)"

输出:"Count:" 因为 $objs.Countnull

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"

推荐答案

即使嵌套数组由一个对象构成,您也可以强制函数返回数组.

You can force the function to return an array even if the nested array is constructed of one object.

function MyFunction
{
    $Objects = @()
    $Objects += [pscustomobject]@{Label = "Hallo"}
    # $Objects += [pscustomobject]@{Label = "World"}
    return ,$Objects
}

即使提供的 $Objects 已经是一个包含多个对象的数组,该逗号也会显式转换为数组.这会强制构造一个具有一个元素的数组.在外面,Powershell 执行 一个单项数组的拆箱,所以如果有一个数组,你会得到一个数组,所以这个方法可以解决默认的 Powershell 行为来处理单项数组.您体验 Count 为 1,因为在 Powershell 3.0 Microsoft 通过向每个对象添加 Count 属性 来修复单项数组,以便一个对象的索引返回 Count 为 1,对于 $null 返回零,但 PSCustomObject 被排除在外,因为自然原因声明它们可以包含自己的 Count 属性,因此自定义对象不包含默认的 Count 属性.这就是为什么如果只有一个对象就不会得到 Count 的原因.

That comma makes an explicit conversion to an array even if the supplied $Objects is already an array of more than one object. This forces a construction of an array with one element. Outside, Powershell does unboxing of a one-item array, so you'll get an array if there was one, so this method works around default Powershell behavior for working with one-item arrays. You experience Count being 1 because in Powershell 3.0 Microsoft fixed one-item arrays by adding Count property to every object so that indexing of one object returns Count as 1, and for $null zero is returned, but the PSCustomObjects are excluded from this as a natural reason declares that they can contain their own Count properties, thus no default Count property is included for a custom object. That's why you don't get Count if there's only one object.

, 的行为可以在下面的例子中看到:

The behavior of , can be seen on the following example:

function hehe {
    $a=@()
    $a+=2
    $a+=4
    $b=,$a
    write-verbose $b.count # if a variable is assigned ",$a", the value is always 1
    return ,$a
}

输出:

PS K:>(呵呵).计数

PS K:> (hehe).count

详细:1

2

这篇关于使用 pscustomobjects 在 PowerShell 中计算数组的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆