从数组中获取PSCustomObject类型的值

时间:2019-02-22 10:13:36

标签: powershell

我正在尝试从PSCustomObject获取值,但找不到正确的方法。

PS: $val
  entry1 : @{order=10; isConditionalDeploy=1; isDropExtendedProperties=0}
  entry2 : @{order=20; isConditionalDeploy=1; isDropExtendedProperties=0}
  entry3 : @{order=30; isConditionalDeploy=1; isDropExtendedProperties=0}

PS: $val.GetType()
  IsPublic IsSerial Name     BaseType
  -------- -------- ----     --------
  True     True     Object[] System.Array

PS: $val[0]
  entry1 : @{order=10; isConditionalDeploy=1; isDropExtendedProperties=0}
  entry2 : @{order=20; isConditionalDeploy=1; isDropExtendedProperties=0}
  entry3 : @{order=30; isConditionalDeploy=1; isDropExtendedProperties=0}

PS: $val[0].GetType()
  IsPublic IsSerial Name           BaseType
  -------- -------- ----           --------
  True     False    PSCustomObject System.Object

我尝试过的所有方法,总是得到相同的结果。我试图从$val.PSObject获取值,但是没有运气

更新:

$val | gm
Name              MemberType   Definition
----              ----------   ----------
Equals            Method       bool Equals(System.Object obj)
GetHashCode       Method       int GetHashCode()
GetType           Method       type GetType()
ToString          Method       string ToString()
entry1            NoteProperty System.Management.Automation.PSCustomObject entry1=@{order=10; isConditionalDeploy=1;
entry2            NoteProperty System.Management.Automation.PSCustomObject entry2=@{order=20; isConditionalDeploy=1; 
entry3            NoteProperty System.Management.Automation.PSCustomObject entry3=@{order=30; isConditionalDeploy=1; 

3 个答案:

答案 0 :(得分:1)

如果您只是尝试访问这些值,则应该可以:

$json = '{
"entry1":
    {
        "order":"10",
        "isConditionalDeploy":"1",
        "isDropExtendedProperties":"0"
    },
"entry2":
    {
        "order":"20",
        "isConditionalDeploy":"1",
        "isDropExtendedProperties":"0"
    }
}'

$val = $json | ConvertFrom-Json

$val | ForEach-Object {
    $_.PSObject.Properties.Value
}

输出

order isConditionalDeploy isDropExtendedProperties
----- ------------------- ------------------------
10    1                   0
20    1                   0

答案 1 :(得分:0)

为了列出所有属性名称,请运行以下命令:

$val.psobject.properties.name

为了列出所有属性值,请运行以下命令:

$val.psobject.properties.value

由于$val.psobject.properties.name$val.psobject.properties.name是数组,因此它们的元素可通过索引访问。因此,如果您想要名字和名字,可以通过$val.psobject.properties.name[0]$val.psobject.properties.value[0]来访问它们。

如果您已经知道属性名称并且只需要值,则可以按照Theo的建议访问值。

我制作了一个JSON文件,并将其命名为json.json。它包含以下内容:

{
"entry1":
    {
        "order":"10",
        "isConditionalDeploy":"1",
        "isDropExtendedProperties":"0"
    },
"entry2":
    {
        "order":"20",
        "isConditionalDeploy":"1",
        "isDropExtendedProperties":"0"
    }
}

以下结果将创建一个看起来像您的$val变量:

$val = Get-Content json.json | ConvertFrom-Json
$val | fl

entry1 : @{order=10; isConditionalDeploy=1; isDropExtendedProperties=0}
entry2 : @{order=20; isConditionalDeploy=1; isDropExtendedProperties=0}

$val | gm

   TypeName: System.Management.Automation.PSCustomObject

   TypeName: System.Management.Automation.PSCustomObject

    Name        MemberType   Definition
    ----        ----------   ----------
    Equals      Method       bool Equals(System.Object obj)
    GetHashCode Method       int GetHashCode()
    GetType     Method       type GetType()
    ToString    Method       string ToString()
    entry1      NoteProperty Object[] entry1=System.Object[]
    entry2      NoteProperty Object[] entry2=System.Object[]

完成上述所有操作后,我的财产名称和值检索即按我的建议进行。您能提供您的JSON文件内容吗?

答案 2 :(得分:0)

如果您有一个PSCustomObject数组,则应该可以将该数组通过管道传递到Format-List或Format-Table。